You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
571 B

2 years ago
package class28;
public class Problem_0012_IntegerToRoman {
public static String intToRoman(int num) {
String[][] c = {
{ "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },
{ "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },
{ "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },
{ "", "M", "MM", "MMM" } };
StringBuilder roman = new StringBuilder();
roman
.append(c[3][num / 1000 % 10])
.append(c[2][num / 100 % 10])
.append(c[1][num / 10 % 10])
.append(c[0][num % 10]);
return roman.toString();
}
}