In: Computer Science
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
import java.util.Scanner;
public class IntToRoman {
public static void main(String[] args) {
//taking input from the user
Scanner sc = new Scanner
(System.in);
System.out.print("Enter the integer: ");
int num = sc.nextInt();
//checking the condition to make sure the number
between 1 to 3999
if ((num < 1 || (num > 3999)))
System.out.println("INVALID");
else System.out.println(convertToRoman(num));
}
//logic to convert integer to roman without using
arrays or loops
public static String convertToRoman(int num){
if (num >= 1000) return "M" + convertToRoman(num -
1000);
if (num >= 900) return "CM" + convertToRoman(num -
900);
if (num >= 500) return "D" + convertToRoman(num -
500);
if (num >= 400) return "CD" + convertToRoman(num -
400);
if (num >= 100) return "C" + convertToRoman(num -
100);
if (num >= 90) return "XC" + convertToRoman(num -
90);
if (num >= 50) return "L" + convertToRoman(num -
50);
if (num >= 40) return "XL" + convertToRoman(num -
40);
if (num >= 10) return "X" + convertToRoman(num -
10);
if (num >= 9) return "IX" + convertToRoman(num -
9);
if (num >= 5) return "V" + convertToRoman(num -
5);
if (num >= 4) return "IV" + convertToRoman(num -
4);
if (num >= 1) return "I" + convertToRoman(num -
1);
return "";
}
}