Home >Java >javaTutorial >JAVA program to convert Roman numerals to integer numbers
Roman Numerals - Based on the ancient Roman system, using symbols to represent numbers. These numbers are called Roman numerals. The symbols are I, V, X, L, C, D and M, representing 1, 5, 10, 50, 100, 500 and 1,000 respectively.
Integer - An integer is an integer consisting of positive, negative and zero values. Fractions are not whole numbers.
Here we set the symbol value based on the integer value. Whenever a Roman numeral is given as input, we divide it into units and then calculate the appropriate Roman numeral.
I - 1 II – 2 III – 3 IV – 4 V – 5 VI – 6 . . . X – 10 XI – 11 . . XV - 15
In this article, we will learn how to convert Roman numerals to integers in Java.
Input Roman number is XIV. Converting it to Integer = 14.
Input Roman number is CCXXXIV. Converting it to Integer = 234.
Input Roman number is MCCXXXI. Converting it to Integer = 1231.
Step-1 - Get input Roman numerals in string form via static input or user input.
Step-2 - In a user-defined method, we declare some conditions where there are Roman numerals with appropriate integer values.
Step-3 - In another user defined method, we calculate the Roman numeral value by using the index value of the given string.
Step-4 - After getting the integer, we print it as output.
We provide solutions in different ways.
Through user-defined methods with static input values.
Through user-defined methods and user input values.
Let’s look at the program and its output one by one.
In this method, we declare a Roman input number through the static input method and pass the number as a parameter to the user-defined method, and then use an algorithm inside the method to convert the Roman number to an integer.
import java.util.*; import java.io.*; import java.lang.Math; public class Main { public static void main(String args[]) { Main obj = new Main(); String inputRoman= "LXVII"; System.out.println("The Integer value of given Roman number is: "+obj.romanToInt(inputRoman)); } int NumValue(char rom) { if (rom == 'I') return 1; if (rom == 'V') return 5; if (rom == 'X') return 10; if (rom == 'L') return 50; if (rom == 'C') return 100; if (rom == 'D') return 500; if (rom == 'M') return 1000; return -1; } int romanToInt(String str) { int sum = 0; for (int i=0; i<str.length(); i++) { int s1 = NumValue(str.charAt(i)); if (i+1 <str.length()) { int s2 = NumValue(str.charAt(i+1)); if (s1 >= s2) { sum = sum + s1; } else{ sum = sum - s1; } } else { sum = sum + s1; } } return sum; } }
The Integer value of given Roman number is: 67
In this method, we declare a Roman input number through the user input method and pass the number as a parameter to the user-defined method, and then use an algorithm inside the method to convert the Roman number to an integer. p>
import java.util.*; import java.io.*; import java.lang.Math; public class Main { public static void main(String args[]) { Main obj = new Main(); Scanner sc = new Scanner(System.in); System.out.print("Enter a Roman Number in capital letters: "); String inputRoman= sc.nextLine(); System.out.println("The Integer value of given Roman number is:"+obj.romanToInt(inputRoman)); } int NumValue(char rom){ if (rom == 'I') return 1; if (rom == 'V') return 5; if (rom == 'X') return 10; if (rom == 'L') return 50; if (rom == 'C') return 100; if (rom == 'D') return 500; if (rom == 'M') return 1000; return -1; } int romanToInt(String str) { int sum = 0; for (int i=0; i<str.length(); i++) { int s1 = NumValue(str.charAt(i)); if (i+1 <str.length()) { int s2 = NumValue(str.charAt(i+1)); if (s1 >= s2) { sum = sum + s1; } else { sum = sum - s1; } } else { sum = sum + s1; } } return sum; } }
Enter a Roman Number in capital letters: V The Integer value of given Roman number is: 5
In this article, we explored how to convert Roman numerals to integers in Java using different methods.
The above is the detailed content of JAVA program to convert Roman numerals to integer numbers. For more information, please follow other related articles on the PHP Chinese website!