Share
Explore BrainMass

Java solution to convert decimal to a given base using recursion

Dear OTA -

I need a Java solution that can read a decimal integer and convert it to binary or any other given base system using recursion. For example, the Java program will do as follows:

read a decimal Integer, val, until "q" is read

if "q" then stop

else read another Integer, base, which will be a representation base

call a method, Converting (val, base), recursively

Note that the method Converting (a,b) will do recursively:
Converting (a,b)
if a is dividable by b
compute the remainder r and the quotient q
call the method Converting (q,b)
print r
else print a

The sample run should look as follows:

Enter an Integer or 'q' to quit:
8
An Integer for the representation base:
2
The given number 8 is converted to 1000 in the base 2 representation.
Enter an Integer or 'q' to quit:
21
An Integer for the representation base:
5
The given number 21 is converted to 41 in the base 5 representation.
Enter an Integer or 'q' to quit:
21
An Integer for the representation base:
7
The given number 21 is converted to 30 in the base 7 representation.
Enter an Integer or 'q' to quit:
21
An Integer for the representation base:
8
The given number 21 is converted to 25 in the base 8 representation.
Enter an Integer or 'q' to quit:
21
An Integer for the representation base:
9
The given number 21 is converted to 23 in the base 9 representation.
Enter an Integer or 'q' to quit:
q

Solution Summary

A Java solution to convert decimals to a given base using recursion is examined.

$2.19