
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
JAVA Program To Convert Roman Number to Integer Number
Roman Number − Based on the ancient Roman system, symbols are used to represent the numbers. These numbers are known as Roman numbers. Symbols are I, V, X, L, C, D, and M use respectively for 1, 5, 10, 50, 100, 500, and 1,000.
Integer Number − Integer number is nothing but the whole number which consists ofpositive, negative and zero value. Fraction numbers are not integers.
Here we have set the symbol values as per their integer values. Whenever a roman number is entered as input we divide into units and then calculate the appropriate roman number.
I - 1 II – 2 III – 3 IV – 4 V – 5 VI – 6 . . . X – 10 XI – 11 . . XV - 15
In this article we will see how to convert a roman number to integer number in Java.
To show you some instances −
Instance-1
Input Roman number is XIV. Converting it to Integer = 14.
Instance-2
Input Roman number is CCXXXIV. Converting it to Integer = 234.
Instance-3
Input Roman number is MCCXXXI. Converting it to Integer = 1231.
Algorithm
Step-1 − Get the input roman number as a string either by static input or user input.
Step-2 − In one user defined method we declared some conditions, where the roman numbers with their appropriate integer values are present.
Step-3 − In another user defined method by using the index value of a given string we calculate the roman number values.
Step-4 − After getting the Integer number, we print this as output.
Multiple Approaches
We have provided the solution in different approaches.
By User Defined Method with Static Input Values.
By User Defined Method with User Input Values.
Let’s see the program along with its output one by one.
Approach-1: By Using User Defined Method with Static Input Value
In this approach, we declare a Roman input number by static input method and pass this number as a parameter in a user-defined method, then inside the method by using the algorithm we can convert the Roman number into an Integer number.
Example
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; } }
Output
The Integer value of given Roman number is: 67
Approach-2: By Using User Input Value
In this approach, we declare a Roman input number by user input method and pass this number as a parameter in a user defined method, then inside the method by using the algorithm we can convert the Roman number into Integer number.
Example
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; } }
Output
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 number to integer number in Java by using different approaches.
- Related Articles
- Java Program to convert decimal integer to octal number
- Java Program to convert decimal integer to hexadecimal number
- Program to convert roman numeral to integer in Python?
- Program to convert integer to roman numeral in Python
- Convert decimal integer to hexadecimal number in Java
- Java program to convert decimal number to hexadecimal number
- Java Program to convert binary number to decimal number
- Java Program to convert hexadecimal number to decimal number
- Java Program to convert octal number to decimal number
- How to convert a decimal number to roman using JavaScript?
- Number to Roman Numerals
- Java Program to convert integer to octal
- Java Program to convert integer to hexadecimal
- Java Program to convert integer to boolean
- Java Program to convert boolean to integer
