#include <stdio.h>
#include <math.h>
int main(){
int base5_v;
int digit = 0;
printf("> ");
scanf("%d", &base5_v);
int remainder = base5_v % 10;
base5_v /= 10;
int base10_v = remainder * (int) powf(5, digit++);
remainder = base5_v % 10;
base5_v /= 10;
base10_v += remainder * (int) powf(5, digit++);
remainder = base5_v % 10;
base10_v += remainder * (int) powf(5, digit++);
printf("%d in base 5 is %d in base 10\n", base5_v, base10_v);
return 0;
}
So I am having a hard time finding the issue in this code. the output is supposed to be like this after I input my number:
144 in base 5 is 49 in base 10
but when I compiled and ran the code it looked like this:
1 in base 5 is 49 in base 10
Can I get some help on what is wrong and how I can fix it?
Your code modifies
base5_veach time it runsbase5_v /= 10;.In your example, you pass
144as an input. The first time, it modified the value as14after the modulus operation, the second time1is left after modulus.I think the biggest mistake in your code is not to use any loop. What'd happen if the user would enter 1234 or another digit length? There are many ways how you could have coded it, but this is one of them (ignoring the user input validation):