fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7.  
  8. char s[40];
  9. int base;
  10. int index,n,p,sum=0; /* n is the number of digits in the converted value */
  11.  
  12. printf("enter the number and base: ");
  13. scanf("%s %d",s,&base);
  14. printf("\nYou entered %s %d\n", s, base);
  15.  
  16. p=strlen(s)-1;
  17. for(index=0; index < strlen(s); index++, p--)
  18. {
  19. sum += (s[index]-'0') * pow(base,p);
  20. }
  21. printf("decimal no. is %d\n",sum);
  22. return 0;
  23.  
  24. }
Success #stdin #stdout 0s 1792KB
stdin
1101 2
stdout
enter the number and base: 
You entered 1101 2
decimal no. is 13