Question: In your sample files, you will find a program named ConvertBase which recursively converts a decimal number to your choice of other bases. Current output
In your sample files, you will find a program named ConvertBase which recursively converts a decimal number to your choice of other bases. Current output looks like:
Enter any decimal number:17 Enter the base you wish to use 2 17 base ten converted to base 2 is 10001
Alter this program (keep it recursive) so that you ask the user for original base as well. Give them a choice of binary, octal, decimal, or hexadecimal. Your program should be able to convert a number from any one of those four bases to the desired one.
Sample Code"Convert Base"
public class ConvertBase
{
public static void main(String[] args)
{ String ans; Scanner s = new Scanner(System.in); System.out.print("Enter any decimal number:"); int num = s.nextInt(); System.out.println("Enter the base you wish to use"); int base = s.nextInt(); ans = binary(num, base); System.out.println(num + " base ten converted to base " + base + " is " + ans ); }
public static String binary(int y, int base) { int rem; if(y > 0) { rem = y % base; return (binary(y / base, base) + "" +rem); } else return ""; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
