// covert from decimal to base b #include #include #include // use math function floor, ceil #define Size 100 static int a[Size] = {0}; // *hold base b representation; initialized to 0 // static: allow to use in different blocks void DecToBase_b(int n, int b) // function converting a positive integer to base b form { int q = n; // if passing by reference, required int k = 0; while (q != 0) { a[k] = q % b; q = (int) floor(q/b); // floor receives double and returns double k++; // k = k + 1; } a[Size] = k - 1; // *hold length of base b representation } void main(void) { int decimal, base; int continued = 1; while (continued) { cout << "enter a positive decimal number and a base, in order: "; cin >> decimal >> base; DecToBase_b(decimal, base); cout << "base " << base << " representation = "; for (int i = a[Size]; i >= 0; i--) // output base b form cout << a[i] << " "; cout << endl; cout << "continue? enter 1 if yes, 0 if not: " << endl; // command cin >> continued; cout << endl; } cout << "enter a character to exit" << endl; getchar(); }