Home > Java > javaTutorial > body text

JAVA program to convert Roman numerals to integer numbers

WBOY
Release: 2023-08-25 11:41:20
forward
857 people have browsed it

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
Copy after login

In this article, we will learn how to convert Roman numerals to integers in Java.

Show you some examples -

Example 1

Input Roman number is XIV.
Converting it to Integer = 14.
Copy after login

Example 2

Input Roman number is CCXXXIV.
Converting it to Integer = 234.
Copy after login

Example 3

Input Roman number is MCCXXXI.
Converting it to Integer = 1231.
Copy after login

algorithm

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.

Multiple methods

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.

Method 1: Using a user-defined method with static input values

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.

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;
   }
} 
Copy after login

Output

The Integer value of given Roman number is: 67
Copy after login

Method 2: Using user input value

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.

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;
   } 
} 
Copy after login

Output

Enter a Roman Number in capital letters: V
The Integer value of given Roman number is: 5
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!