#include #include #define MAX_DIGITS 50 #define PRECISION 6 void printDigit(int d) { if ( d < 10 ) { printf("%d",d); } else if ( d < 16 ) { putchar('a' + (d-10)); } else { printf("<%d>",d); } } int main(void) { /* variables I need */ double f; int n, b, col; int digits[MAX_DIGITS]; printf("f (floating point input)? "); scanf("%lf",&f); printf("b? "); scanf("%d",&b); /* first, deal with the integer part as before */ n = (int)f; col = 0; if ( n == 0 ) { digits[0] = 0; col++; } else { while ( n > 0 ) { digits[col] = n%b; n = n/b; col++; } } col--; /* now we need to print the digits */ while ( col >= 0 ) { printDigit(digits[col]); col--; } /* and the "decimal" point */ printf("."); /* now we can simply loop, taking the fractional part of f, multiplying by b, and taking the integer part of that as the next digit. We can stop after printing column -PRECISION. */ while ( col >= -PRECISION ) { f = f - (int)f; f *= b; n = (int)f; printDigit(n); col--; } printf("\n"); return EXIT_SUCCESS; }