Converting Roman to Integer Numerals in java
In this article, you will be acknowledged about the process of conversion of roman numbers to integers in java. The most important approaches are discussed and also the implementation of programs that help in converting integers to roman numerals.
It is a question that is regularly asked in job interviews at prestigious IT firms like Google, Amazon, TCS, Accenture, etc. By figuring out the solution, one may assess the interviewee's logical reasoning, critical reasoning, and problem-solving abilities. Therefore, we will explore various methods and logical processes for converting roman numbers to numbers in Java in this part. We will also develop Java apps for the same.
Roman Numerals
Roman numerals are used to represent numbers symbolically. These are typically employed in music theory, watch faces, etc. Roman numerals are represented by seven letters. The roman numbers and related decimal values are shown in the following table.
Character | Roman Numeral |
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Roman numbers possess the fundamental characteristics.
With a few exceptions, it is typically written highest to lowest, left to right (the right character is greater than the left character in this case.). For instance, IV is identical to 4 in roman numerals.
In this situation, we deduct the right character values from the left character value. IV, for instance, will be 5-1=4. IX will also be 10-1=9 in the same way.
Such properties are:
- Roman numeral I, which stands for "subtract one," can be used before V or X. For instance, 9 is IX (10-1) = 9 and IV (5-1) = 4.
- L or C may come before the roman number X to indicate a ten-subtraction. XL (50-10) equals 40, for instance, while XC (100-10) equals 90.
- Before the roman numerals D or M, the letter C stands for the hundredth subtracted. CM (1000-100) = 900 and CD (500-100) = 400, respectively.
Example :
Let's say we need to translate MCMXC from roman number to integer. We will write a corresponding value for each roman number and add them all up to obtain the integer value. As a result, 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
MCMXC thus represents 1990.
Methodology
- Perform iterations over each character in the provided Roman number string.
- The value of the present Roman character should be compared to its correct Roman character.
- Add the value of the current character to the total variable if it exceeds or is equivalent to the value of such symbol to the right.
- Subtract the value of the current character from the overall variable if it is lower than the value of such symbol to the right.
Let us understand it with a simple example program
File name: Roman.java
// Roman Numerals to Numbers Conversion in Java
import java.util.*;
public class Roman {
// A Roman symbol's value is returned by this method.
int value(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
// determines a given roman numeral's decimal value
int romanToDecimal(String str)
{
// result initialization
int res = 0;
for (int i = 0; i < str.length(); i++) {
int s1 = value(str.charAt(i));
if (i + 1 < str.length()) {
int s2 = value(str.charAt(i + 1));
// comparing the two numbers
if (s1 >= s2) {
// The present symbol's value is higher than or equal to that of the following symbol.
res = res + s1;
}
else {
// Value of the present symbol is smaller than that of the following symbol.
res = res + s2 - s1;
i++;
}
}
else {
res = res + s1;
}
}
return res;
}
public static void main(String args[])
{
Roman ob = new Roman();
// assuming the provided inputs are valid
String str = "MDCL";
System.out.println("Integer form of the given Roman Numeral"
+ " is "
+ ob.romanToDecimal(str));
}
}
Output
Integer form of the given Roman Numeral is 1650
The above method has an O(n) time and space complexity, where n is the size of the supplied roman numeral string.