-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntergerToRoman.java
More file actions
32 lines (26 loc) · 810 Bytes
/
IntergerToRoman.java
File metadata and controls
32 lines (26 loc) · 810 Bytes
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
public class IntergerToRoman {
public static String intToRoman(int num) {
int[] values = {
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4, 1
};
String[] symbols = {
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV", "I"
};
StringBuilder result = new StringBuilder();
for (int i = 0; i < values.length; i++) {
while (num >= values[i]) {
num -= values[i];
result.append(symbols[i]);
}
}
return result.toString();
}
public static void main(String[] args) {
int num = 1995;
System.out.println("Roman: " + intToRoman(num));
}
}