I need a recursive program that converts a base 10 number to another base any ba
ID: 3623877 • Letter: I
Question
I need a recursive program that converts a base 10 number to another base any base between 2 and 36. this is what I have so farimport java.util.Scanner;
public class Lab7
{
public void Conversion(int base, int cBase)
{
String result;
int S = 0;
result="";
if (base<cBase)
result = result + base;
else
Conversion(base/cBase,cBase);
result = result+(base%cBase);
}
public static void main(String[]args)
{
int base;
int cBase;
Scanner input = new Scanner(System.in);
System.out.println("Enter a base 10 ");
base = input.nextInt();
System.out.println("Enter which base you want to convert to: 2 through 36 ");
cBase = input.nextInt();
}
}
Explanation / Answer
import java.util.*;
public class BaseConversion {
public static String[] numberStrings = new String [] {
"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H",
"I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
};
public static String conversion(int number, int base) {
if (number == 0) {
return "";
}
return conversion(number/base, base) + numberStrings[number % base];
}
public static void main(String[] args) {
int number;
int cBase;
Scanner input = new Scanner(System.in);
System.out.println("Enter a number in base 10 ");
number = input.nextInt();
input.nextLine();
System.out.println("Enter which base you want to convert to: 2 through 36 ");
cBase = input.nextInt();
input.nextLine();
System.out.println("The base "+cBase+" representation of "+number+ " is "+
conversion(number,cBase));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.