-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerToRoman.java
More file actions
28 lines (21 loc) · 891 Bytes
/
Copy pathIntegerToRoman.java
File metadata and controls
28 lines (21 loc) · 891 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
package LeetcodePractice;
public class IntegerToRoman {
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 roman = new StringBuilder();
for (int i = 0; i < values.length; i++) {
while (num >= values[i]) {
roman.append(symbols[i]);
num -= values[i];
}
}
return roman.toString();
}
public static void main(String[] args) {
int num1 = 3749;
System.out.println("Roman numeral of " + num1 + " is " + intToRoman(num1));
int num2 = 58;
System.out.println("Roman numeral of " + num2 + " is " + intToRoman(num2));
}
}