In the Decimal to Binary Conversion programming example presented in this chapter, you learned how to convert a decimal number into its equivalent binary number. Two more number systems, octal (base 8) and hexadecimal (base 16), are of interest to computer scientists.
The digits in the octal number system are 0, 1, 2, 3, 4, 5, 6, and 7. The digits in the hexadecimal number system are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. So, A in hexadecimal is 10 in decimal, B in hexadecimal is 11 in decimal, and so on.
The algorithm to convert a positive decimal number into an equivalent number in octal (or hexadecimal) is the same as that discussed for binary numbers. Here, we divide the decimal number by 8 (for octal) and by 16 (for hexadecimal). Suppose that ab represents the number a to the base b. For example, 7510 means 75 to the base 10 (that is, decimal), and 8316 means 83 to the base 16 (that is, hexadecimal). Then:
75310 = 13618
75310 = 2F116
The method of converting a decimal number to base 2, or 8, or 16 can be extended to any arbitrary base. Suppose you want to convert a decimal number n into an equivalent number in base b, where b is between 2 and 36. You then divide the decimal number n by b, as in the algorithm for converting decimal to binary.
Note that the digits in, say, base 20, are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, G, H, I, and J.
Write a program that uses a recursive method to convert a number in decimal to a given base b, where b is between 2 and 36. Your program should prompt the user to enter the number in decimal and in the desired base.
Test your program on the following data:
9098 and base 20
692 and base 2
753 and base 16
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.