/* * Display a value in a specified base between 2 and 10. */ #include #include void displayValueInBase(unsigned long v, unsigned int b) { unsigned int k; /* digits needed in result */ unsigned long divisor; /* initially b^(# of digits - 1) */ if (v == 0) /* zero is the same in any base */ printf("0"); else { k = floor(log10(v)/log10(b)) + 1; divisor = pow(b, k - 1); /* first divisor is b^(k - 1) */ /* Run through value, calculating and displaying the value of each of the digits in the new base (left to right) */ while (divisor >= 1) { printf("%lu", v / divisor); v = v % divisor; divisor = divisor / b; } } }