code :
/***************************************************************** * Program: print_num.C * * Purpose: implements/illustrates a recursive function for printing a * number in an arbitrary base * * Authors: * *****************************************************************/ #include #include /***************************************************************** * print_num - prints a number in a given base * * calling sequence: * print_num(val, base) * * parameters - * val - the number to be printed * base - the base to print it * * effect - * The number is printed in the given base on the output stream. The letters 'a' * through 'z' are used to represent the "digits" 10 through 35, when applicable. * * restrictions - * Only bases 2 through 36 are handled. However, no checking is done that the * base is in that range, so if you give it a bogus base, some very strange * things might occur. * * example - * print_num(cout, 20, 6) causes a "32" to be printed to standard output (because * 32 in base 6 is 20 (3*6 + 2*1). * * implementation - * If the number is smaller than the base, its character-value is looked up in a table * and is printed directly. Otherwise, the number is divided by the base and printed * (recursively); then the last digit is printed (recursively). * *****************************************************************/ void print_num(int val, int base) { /* this array is used to translate between number values and digit-characters */ char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; if (val = base, so print quotient and then remainder */ // this case works int quotient = val / base; int remainder = val % base; print_num(quotient, base); print_num(remainder, base); } } /***************************************************************** * main - main program to exercise 'print_num' * * This program prompts the user for a number, and then prints the number in bases * 2 through 36. * *****************************************************************/ int main(int argc, char *argv[]) { int i; /* prompt user and read number */ int val; printf("Please type a number: "); scanf("%d", &val); /ote: does not do error-checking /* print the number in all bases from 2 through 36 */ for (i = 2; i Part 4: Number-printing (print num.c) - Fix the base cases (switch driver) 1. Examine the print num function in print num. c. This function is intended to print a number in any given base between 2 and 36. The main function reads an integer and prints it in all bases between 2 and 36. The letters a through z are used to represent the digits 10 through 35, respectivelv The function takes two arguments: the number to print the base to print the number in 2. lts intended operation is as follows: base case 1 (which you need to do) the number is negative, print a - character, and then recursively call the function with the negated number (which becomes a positive number) base case 2 (which you need to do): if the number is less than the base, print its digit, using the digits array. Hint: be sure to print using formatting code %c for character. recursive case (already done): if the number is greater than or equal to the base recursively print the value of the number divided by the base. This should print out all the digits except the last. recursively print the value of the number remaindered by the base. This should print out the last digit. o o 3. For example, to print 173 in base 10: recursively print 173/10: this prints "17" by recursively calling print_num(17, 10) recursively print 173%10: this prints "3" The result is that "173" is printed. 4. This function always terminates because: a negative value always results in a recursive call with a positive value. (We'll ignore MIN INT for now) a positive value always results in either the base case, or recursive calls with smaller values