Javatpoint Logo
Javatpoint Logo

Convert Roman to Integer in Java

It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to discuss how to convert roman numerals to integers in Java with different approaches and logic. Also, we will create Java programs for the same.

Convert Roman to Integer in Java

Roman Numerals

Roman numerals are the symbolic representation of numbers. These are usually used in watch faces, music theory, etc. There are seven letters used to represent roman numerals. The following table represents the roman numerals and corresponding decimal values.

Roman Numeral Integer Values
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

Roman numerals are formed by joining the symbols and adding their respective values. These are usually written in largest to smallest fashion, and from left to right.

However, the roman numeral for the integer 4 is not IIII; instead, it is written as IV. It means that when there is a smaller number placed before a larger number, the values are subtracted; because the 1 is placed before the 5, we can subtract it to get 4.

The same principle applies to the number 9, which is written as IX.

There are six instances where we required subtraction:

  • I can be placed before V (5) and X to make 4 and 9.
  • X can be placed before L (50) and C to make 40 and 90.
  • C can be placed before D (500) and M to make 400 and 900.

Example 1:

Suppose, we have to convert the roman numeral MCMXC to an integer. In order to get the integer value, we will write the corresponding value of each roman numeral and sum up. Therefore, we get:

M=1000, C=100, M=1000, X=10, C=100

M=1000

CM=1000-100 = 900

XC=100-10 = 90

Hence,

M=1000, CM=900, XC=90 = 1990

Hence, MCMXC is equivalent to 1990.

Let's see another example.

Example 2:

Consider the roman numeral LVIII.

L=50, V=5, III=8

L+V+III = 50+5+3 = 58

Algorithm

  • Iterate over each character of the given string of roman numerals.
  • Compare the value of the current roman character with its right roman character.
  1. If the current value is greater than or equal to the value of the symbol to the right, add the current character's value to the total variable.
  2. If the current value is less than the value of the symbol to the right, subtract the current character's value from the total variable.

Let's implement the above steps in a Java program.

Java Program to Convert Roman Numerals to Integer in Java

RomanToInteger1.java

Output:

The corresponding Integer value is: 1155

Complexity

The time and space complexity of the above approach is O(n), where n is the length of the given roman numeral string.

Let's see another solution for the same.

RomanToInteger2.java

Output:

The corresponding Integer value is: 1915

Complexity

The time complexity of the above approach is O(n), where n is the length of the given roman numeral string and the space complexity is O(1).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA