0% found this document useful (0 votes)
10 views1 page

Roman To Integer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Roman To Integer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

public class Main {

public static int romanToInt(String s) {


char[] charArray = s.toCharArray();
int[] result = new int[charArray.length];
int i = 0;
int j = 0;
int sum = 0;

for (i = 0; i < charArray.length; i++) {


if (charArray[i] == 'I') {
result[j] = 1;
} else if (charArray[i] == 'V') {
result[j] = 5;
} else if (charArray[i] == 'X') {
result[j] = 10;
} else if (charArray[i] == 'L') {
result[j] = 50;
} else if (charArray[i] == 'C') {
result[j] = 100;
} else if (charArray[i] == 'D') {
result[j] = 500;
} else if (charArray[i] == 'M') {
result[j] = 1000;
}

j++;
}
for (i = 0; i < result.length - 1; i++) {
if (result[i] < result[i+1]) {
result[i+1] = result[i+1] - result[i];
result[i] = 0;

for (i = 0; i < result.length; i++) {


sum += result[i];
}

return sum;
}

public static void main(String[] args) {


String roman = "XLIV";
System.out.println(romanToInt(roman));
}
}

You might also like