-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegerToRoman.java
86 lines (78 loc) · 1.17 KB
/
IntegerToRoman.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class IntegerToRoman {
public String IntegerToRoman(int num) {
StringBuilder s = new StringBuilder();
while (num > 0) {
if (num >= 1000) {
s.append("M");
num = num - 1000;
continue;
}
else if(num>=900)
{
s.append("DM");
num = num-900;
continue;
}
else if(num>=500)
{
s.append("D");
num-=500;
continue;
}
else if(num>=400)
{
s.append("CD");
num-=400;
continue;
}
else if(num>=100)
{
s.append("C");
num-=100;
continue;
}
else if(num>=50)
{
s.append("LC");
num-=50;
continue;
}
else if(num>=10)
{
s.append("X");
num-=10;
continue;
}
else if(num>=9)
{
s.append("IX");
num-=9;
continue;
}
else if(num>=5)
{
s.append("V");
num-=5;
continue;
}
else if(num>=4)
{
s.append("IV");
num-=4;
continue;
}
else
{
s.append("I");
num-=1;
continue;
}
}
return s.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
IntegerToRoman i = new IntegerToRoman();
System.out.println(i.IntegerToRoman(2390));
}
}