In this article, we look into an interesting problem: `How to convert Roman Number to Integer in Java`

.

Roman numerals are represented by seven different symbols : I, V, X, L, C, D, M. The Integral values associated with each symbol is shown in the table below:

`Roman Symbol` | `Value ` |

I | 1 |

V | 5 |

X | 10 |

L | 50 |

C | 100 |

D | 500 |

M | 1000 |

Usually, roman numbers are written from left to right, which makes it easier for us to scan each roman character and add the corresponding value for each character to get the result.

**💻 Awesome Tech Resources:**

- Looking for ⚒️ tech jobs? Go to our job portal.
- Looking for tech events? Go to tech events 🗓️ Calendar.️

`For Example:`

Roman Number `III`

has Integral value is 3 adding value of three I’s together (1+1+1=3).

Similarly for Roman Number: `DCLXVI`

, Its integer value is : 500 + 100 + 50 + 10 +5 + 1= 666.

However, the Roman Number for 4 and 9 are not written as `IIII`

and `VIIII`

, respectively. They are written as IV which is (5-1 = 4), and for 9 it is `IX`

(10 -1=9). This case is for letter `I`

. There are 2 more cases to consider:

`X`

can be placed before`L`

(50) and C (100), like`XL`

and`XC`

to make 40 and 90 respectively.- Similarly,
`C`

can be placed before`D`

and`M`

, like`CD`

and`CM`

to make 400 and 900, respectively.

Hence, for Roman Number ‘CDXLIX’ the integer is 449.

`Note:`

If a Roman Symbol having smaller value precedes a Symbol with Higher Value we need to subtract the higher value from the smaller value to get the result. Roman Numbers can have maximum Integral value: 3999 represented as : MMMCMXCIX.

## Implementation

- We will take a HashMap and for each Roman Character, we will put its respective value in the Map as shown in the table above. The Key would be the Roman Symbol and the Value will be the Integral Value of the Symbol.
- We iterate through each character in the given Number ,For each character we need to consider two cases :
`Case 1 :`

If the Character’s value is greater than the previous character value i.e. The condition is :`if (Value(charAt(i)) >Value(charAt(i-1))`

, we need to perform subtraction as :`Value(charAt(i)) - 2* Value(charAt(i-1))`

; then add the result to our answer.`Case 2:`

If the Character’s value is lesser than previous one we simply add its corresponding value to our answer and continue the process.

- In Case 1, we subtract twice the Previous Character value because we might have already added the value while processing it so we subtract twice the value.

Now let us have a quick look at the code in 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 |
import java.util.*; class RomanToInteger { public static int romanToInteger(String roman) { Map<Character,Integer> numbersMap = new HashMap<>(); numbersMap.put('I',1); numbersMap.put('V',5); numbersMap.put('X',10); numbersMap.put('L',50); numbersMap.put('C',100); numbersMap.put('D',500); numbersMap.put('M',1000); int result=0; for(int i=0;i<roman.length();i++) { char ch = roman.charAt(i); // Current Roman Character //Case 1 if(i>0 && numbersMap.get(ch) > numbersMap.get(roman.charAt(i-1))) { result += numbersMap.get(ch) - 2*numbersMap.get(roman.charAt(i-1)); } // Case 2: just add the corresponding number to result. else result += numbersMap.get(ch); } return result; } public static void main(String args[]) { // we take input as a String String romanNumber="MCMXCIV"; int result = romanToInteger(romanNumber); System.out.println("The Roman Number is: "+romanNumber); System.out.println("Its Integer Value is: "+result); System.out.println(); romanNumber="DCCXCIX"; result = romanToInteger(romanNumber); System.out.println("The Roman Number is: "+romanNumber); System.out.println("Its Integer Value is: "+result); } } |

`Output:`

1 2 3 4 5 6 7 |
The Roman Number is: MCMXCIV Its Integer Value is: 1994 The Roman Number is: DCCXCIX Its Integer Value is: 799 |

`Time Complexity :`

The time complexity of this code is `O(n)`

, where n is the length of Roman Number in String format as we are traversing String only once.

Convert decimal to binary in java

So that’s all about how to convert Roman Number to Integer in Java.