Create a program to convert a decimal number to a number system from 2 to 16. Converting a decimal value to another number system should use RECURSIVE FUNCTION. Follow the format of the output below. Use your Lastname as the name of the class. Programs that did not use RECURSIVE FUNCTION to solve the program will not be accepted.
Sample Output.
decimal value: m
target base: n
value of m in base n is y
decimal value: a
target base: b
value of a in base b is c
decimal value: -1
thank you for using the program. bye!
I was just wondering if I am doing it right.
import java.util.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("Enter a decimal number: ");
int n = sc.nextInt();
if(n < 2 || n > 16 ){
System.out.println("thank you for using the program. bye!");
break;
}
int number = n;
int stack[]=new int[16];
int i = 0;
while(number > 1){
stack[i++] = number%2;
number = number/2;
}
System.out.print("binary equivalent of "+ n +" is equal to ");
for(int j = i-1;j >= 0;j--)
{
System.out.print(stack[j]);
}
System.out.println(" ");
}
}
}```
question from:
https://stackoverflow.com/questions/65904202/recursive-functiondecimals-from-2-to-16-by-using-java 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…