Question
I need Help with Base Conversion Write a program that uses recursion to convert a base 10 number into a base b number, where b
I need Help with Base Conversion
Write a program that uses recursion to convert a base 10 number into a base b number, where b < 10. If the number to be converted is n, then the algorithm to convert n to base b is:
1) Divide n by b. Store the quotient and the remainder.
2) The remainder is the rightmost digit of the final answer.
3) The quotient is now the new number n that you will recursively convert to base b.
4) Repeat step a by calling your recursive method with the quotient and the original base b.
5) Stop when n / b = 0. The remainder at this point will be the first digit of the final answer.
For example, to convert 30 into a base 4 number:
Quotient Remainder
30/4 7 2
7/4 1 3
1/4 0 1
The answer is the remainder column read bottom to top, so 30 (base 10) = 132 (base 4).
A skeleton of the recursive method is given below.
public static String convert(int number, int base)
{
int quotient =
int remainder =
if( )
return( + );
else
return( ) + );
}
Sample Output:
Enter a number to convert: 48 Enter a base to convert to: 8 48 converted into base 8 is 60.
Enter a number to convert: 157 Enter a base to convert to: 2 157 converted into base 2 is 10011101.
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started