-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.java
42 lines (32 loc) · 1.31 KB
/
Day18.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Scanner;
//number to roman no
public class IntegerToRoman {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter an integer (0 to 3999): ");
int number = scanner.nextInt();
if (number == 0) {
System.out.println("Exiting the program.");
break;
}
if (number < 1 || number > 3999) {
System.out.println("Please enter a valid integer between 1 and 3999 or 0 to exit.");
} else {
String roman = intToRoman(number);
System.out.println("Roman numeral: " + roman);
}
}
scanner.close();
}
public static String intToRoman(int num) {
String[] thousands = {"", "M", "MM", "MMM"};
String[] hundreds = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
String[] tens = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
String[] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return thousands[num / 1000] +
hundreds[(num % 1000) / 100] +
tens[(num % 100) / 10] +
ones[num % 10];
}
}