-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman_to_integer.java
More file actions
38 lines (35 loc) · 1.16 KB
/
Roman_to_integer.java
File metadata and controls
38 lines (35 loc) · 1.16 KB
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
import java.util.Scanner;
public class Roman_to_integer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Roman Number = ");
String str = sc.next();
System.out.println("Value = "+romanToInteger(str));
} public static int getValue(char r) {
switch (r){
case 'I' :return 1;
case 'V' :return 5;
case 'X' :return 10;
case 'L' :return 50;
case 'C' :return 100;
case 'D' :return 500;
case 'M' :return 1000;
default : return 0;
}
} public static int romanToInteger(String str) {
int total = 0;
for(int i = 0; i < str.length(); i++){
int v1 = getValue(str.charAt(i));
if(i + 1 < str.length()){
int v2 = getValue(str.charAt(i + 1));
if(v1 >= v2){
total = total + v1;
} else {
total = total - v1;
}
} else {
total = total + v1;
}
} return total;
}
}