- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program for Converting Roman Numerals to Decimal Lying Between 1 to 3999
The characters used in an arrangement of number notation based on the pre-Roman Roman system is called Roman numerals. The letters M, D, C, L, X, V, and I stand for 1000, 500, 1000, 50, 10, 5, and 1, respectively, and will discuss all main symbols in the below section. In this problem, we are given a string of Roman numerals and our task is to convert Roman numerals to decimals in the range of 1 to 3999.
Let’s see examples with explanations below to understand the problem in a better way.
Input 1
str = "MCMIX"
Output 1
1909
Explanation
M is the Roman representation of 1000,
CM is the Roman representation of 900,
IX is the Roman representation of 9.
Input 2
str = "IV"
Output
2
Explanation
IV is the Roman representation of 4.
Input 3
str = "VI"
Output 3
6
SYMBOL | VALUE |
---|---|
I | 1 |
IV | 4 |
IX | 9 |
V | 5 |
X | 10 |
XL | 40 |
XC | 90 |
L | 50 |
C | 100 |
CD | 400 |
CM | 900 |
D | 500 |
M | 1000 |
Approach
We have seen the example above for the given Roman numeral string, let us move to the approach.
As per the observation, the Roman numeral's symbol follows the descending order to represent the numbers (e.g. C's come first, the X's, etc.). However, it also follows subtractive notation in certain situations to prevent 4 characters from continuously repeated in a row(for example CCCC or IIII):
I comes before V or X to indicate one less.
Example: 4 = IV (1 less than five),
9 = IX ( 1 less than ten)
X comes before L or C to indicate ten less.
Example: 40 = XL (ten less than fifty),
90 = XC (10 less than 100)
C comes before D and M to indicate 100 less
Example: 400 = CD (hundred less than five hundred)
900 = CM (hundred less than thousand)
Let's see the code below for a better understanding of the above approach.
Example
Below is a Java Program to convert Roman Numerals to Decimal Lying Between 1 to 3999.
import java.util.*; // Create a class for initializing the function to return the Roman symbol's value. public class Solution{ // This function is created to return a Roman symbol's value. int romanValue(char ch) { if(ch=='I') return 1; else if(ch=='V') return 5; else if(ch=='X') return 10; else if(ch=='L') return 50; else if(ch=='C') return 100; else if(ch=='D') return 500; else if(ch=='M') return 1000; return -1; } // This function is created for the conversion of Roman numerals to decimal numerals int convertRomanToDecimal(String str) { // Initialize decimal value int decVal = 0; int n = str.length(); // Getting the size of the string for (int i = 0; i < n; i++) { // check if i+1 charchter exist and getting value of roman symbol str[i] and str[i+1] if (i + 1 < n && romanValue(str.charAt(i)) < romanValue(str.charAt(i + 1))) { //subtract the current value from the next value and add the decVal variable decVal = decVal + romanValue(str.charAt(i + 1)) - romanValue(str.charAt(i)); i++; // Increment the index of the string to point to the next char } // If i+1 char not exist else { decVal = decVal + romanValue(str.charAt(i)); // add the first char value } } return decVal; // Return decimal value } public static void main(String args[]) { Solution ob = new Solution(); String str = "MCMIX"; // Given string System.out.println("Roman Numeral: " + str); // Print the decimal form and call the function of conversion System.out.println("The decimal Numeral form of the Roman Numeral" + " is " + ob.convertRomanToDecimal(str)); } }
Output
Roman Numeral: MCMIX The decimal Numeral form of the Roman Numeral is 1909
Time and Space Complexity
The time complexity of the above code is O(N), as only one traverse of the string is required. Where N is the size of the given Roman numeral string. As no extra space is used the space complexity of the above code is O(1).
Another Solution using Hashmap
Example
import java.util.Map; import java.util.HashMap; public class Solution { public static final Map<Character, Integer> romanValue = new HashMap<Character, Integer>() { { put ( 'M', 1000 ); put ( 'D', 500 ); put ( 'C', 100 ); put ( 'L', 50 ); put ( 'X', 10 ); put ( 'V', 5 ); put ( 'I', 1 ); } }; // Function is created for conversion of the Roman numeral to decimal numeral private static int convertRomanToDecimal(String str) { // Initialize decimal value int decVal = 0; for (int i = 0; i < str.length(); i++) { // store numeric value of roman symbol str[i] int first = romanValue.get(str.charAt(i)); // check if i+1 charchter exist or not if (i + 1 < str.length()) { // store numeric value of roman symbol str[i+1] int second = romanValue.get(str.charAt(i + 1)); // check which value is greater first or second if (first <= second) { // if first value <= second add first value to variable decVal decVal = decVal + first; } else { // Value of first char is less than to the the second char decVal = decVal + second - first; i++; // Increment the index of string to point to next char } } // If i+1 char not exist else { decVal = decVal + first; // add the first char value } } return decVal; // Return decimal value } public static void main(String args[]) { String str = "MMMIX"; // Given string System.out.println("Roman Numeral: " + str); // print the decimal form and call function of conversion System.out.println("Decimal Numeral form of Roman Numeral" + " is " + convertRomanToDecimal(str)); } }
Output
Roman Numeral: MMMIX Decimal Numeral form of Roman Numeral is 3009
Conclusion
In this tutorial, we have implemented a Java program for Converting Roman Numerals to Decimal Lying Between 1 to 3999. We have implemented an approach with two solutions. First with normal function and second with hashmap function.