/* This program prints out the digits of an integer in a certain base. (Here base = 16). It is accomplished by a recursive function call of the function printbase. */ #include void printbase(int, int); void main() { int base=16; int num; cout << "Please enter an integer: "; cin >> num; printbase(num, base); cout << endl; } /* printbase is called recursively. Note that since the cout statement which prints out the digit is called AFTER the recursive function call, the digits are printed out in the correct order. This fixes the problem that it is easy to compute the LAST digit of a number, but we must print out the FIRST digit first. */ void printbase(int n, int b) { char outp; // cout << "n is " << n << ". b is " << b << ".\n"; if (n<0){ cout << '-'; n=-n; } if (n/b) printbase(n/b, b); if (n%b < 10) outp = n%b + '0'; else outp = n%b - 10 + 'A'; cout << outp; /* prints out the digit n%b */ }