Convert Roman Number to Decimal | String Problem

4 years ago Lalit Bhagtani 0

Problem Statement

Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M.

Symbol  --> Value
I  --> 1
V  --> 5
X  --> 10
L  --> 50
C  --> 100
D  --> 500
M  --> 1000

For example,

  1. Two is written as II in Roman numeral, just two one’s added together.
  2. Twelve is written as, XII, which is simply X + II.
  3. Twenty Seven is written as XXVII, which is XX + V + II.

Roman numerals are written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five, we subtract one from five to make four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  1. I can be placed before V (5) and X (10) to make it 4 and 9.
  2. X can be placed before L (50) and C (100) to make it 40 and 90.
  3. C can be placed before D (500) and M (1000) to make it 400 and 900.

You have given a roman numeral, convert it into an decimal integer.

Note: Input roman numeral is within the range from 1 to 3999.

Example 

Convert Roman Number to Decimal

Input :- "IV"
Output :- 4
Input :- "LVIII"
Output :- 58

Solution

To solve this problem, traverse the input roman numeral string. At each iteration, find out the decimal integer equivalent of each roman numeral character by handling special condition of numeral four. Add all the decimal integer and return the result.

Program

public class Main {

	public static void main(String[] args) {		
		Main main = new Main();
		int result = main.romanToInt("LVIII");
		System.out.println(result);
	}
	
	/* Solution */
	public int romanToInt(String s) {
        char[] array = s.toCharArray();
        int sum = 0;
        for(int i=0; i<array.length; i++){
            
            switch(array[i]){
                case 'I':
                    if(i+1 < array.length){
                        if(array[i+1] == 'V'){
                            sum = sum + 4;
                            i++;
                        }else if(array[i+1] == 'X'){
                            sum = sum + 9;
                            i++;
                        }else {
                            sum = sum + 1;
                        }
                    }else {
                        sum = sum + 1;
                    }                    
                    break;
                case 'V':
                    sum = sum + 5;
                    break;
                case 'X':
                    if(i+1 < array.length){
                        if(array[i+1] == 'L'){
                            sum = sum + 40;
                            i++;
                        }else if(array[i+1] == 'C'){
                            sum = sum + 90;
                            i++;
                        }else {
                            sum = sum + 10;
                        }
                    }else {
                        sum = sum + 10;
                    }
                    break;
                case 'L':
                    sum = sum + 50;
                    break;
                case 'C':
                    if(i+1 < array.length){
                        if(array[i+1] == 'D'){
                            sum = sum + 400;
                            i++;
                        }else if(array[i+1] == 'M'){
                            sum = sum + 900;
                            i++;
                        }else {
                            sum = sum + 100;
                        }
                    }else {
                        sum = sum + 100;
                    }
                    break;
                case 'D':
                    sum = sum + 500;
                    break;
                case 'M':
                    sum = sum + 1000;
                    break;
            }
        }
        return sum;
    }
}

Result

58

Similar Post

  1. Single Row Keyboard
  2. Alphabet Board Path
  3. Detect Capital in String

That’s all for Convert Roman Number to Decimal in Java, If you liked it, please share your thoughts in a comments section and share it with others too.