Skip to content

Commit ce676de

Browse files
author
Kartik Siddhabathula
committed
Program to convert Roman numerals to Decimal i.e. Integer number
1 parent e9ec41b commit ce676de

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

ArraysStrings/RomanToInt.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package programcreek.ArraysStrings;
2+
3+
/*
4+
* author: Kartik
5+
* Program to convert Roman numerals to Decimal i.e. Integer number
6+
*/
7+
8+
import java.util.Map;
9+
10+
public class RomanToInt {
11+
12+
public static void main(String[] args) {
13+
String roman = "LIX";
14+
int num = romanToInteger(roman);
15+
System.out.println(num);
16+
17+
}
18+
19+
private static int romanToInteger(String s) {
20+
Map<Character, Integer> map = Map.of('I',1, 'V',5,'X',10,'L',50,'C',100,'D',500,'M',1000);
21+
int sum = map.get(s.charAt(s.length()-1));
22+
for(int i=s.length()-2;i>=0;i--) {
23+
if(map.get(s.charAt(i)) < map.get(s.charAt(i+1))) {
24+
sum -= map.get(s.charAt(i));
25+
}else {
26+
sum += map.get(s.charAt(i));
27+
}
28+
}
29+
return sum;
30+
}
31+
32+
}

0 commit comments

Comments
 (0)