search
HomeJavajavaTutorialJAVA program to convert Roman numerals to integer numbers

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.

Show you some examples -

Example 1

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

Example 2

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

Example 3

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

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;
   }
} 

Output

The Integer value of given Roman number is: 67

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. p>

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 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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Why can't JavaScript directly obtain hardware information on the user's computer?Why can't JavaScript directly obtain hardware information on the user's computer?Apr 19, 2025 pm 08:15 PM

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

Circular dependencies appear in the RuoYi framework. How to troubleshoot and solve the problem of dynamicDataSource Bean?Circular dependencies appear in the RuoYi framework. How to troubleshoot and solve the problem of dynamicDataSource Bean?Apr 19, 2025 pm 08:12 PM

RuoYi framework circular dependency problem troubleshooting and solving the problem of circular dependency when using RuoYi framework for development, we often encounter circular dependency problems, which often leads to the program...

When building a microservice architecture using Spring Cloud Alibaba, do you have to manage each module in a parent-child engineering structure?When building a microservice architecture using Spring Cloud Alibaba, do you have to manage each module in a parent-child engineering structure?Apr 19, 2025 pm 08:09 PM

About SpringCloudAlibaba microservices modular development using SpringCloud...

Treatment of x² in curve integral: Why can the standard answer be ignored (1/3) x³?Treatment of x² in curve integral: Why can the standard answer be ignored (1/3) x³?Apr 19, 2025 pm 08:06 PM

Questions about a curve integral This article will answer a curve integral question. The questioner had a question about the standard answer to a sample question...

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot?What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot?Apr 19, 2025 pm 08:03 PM

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

Why can't the main class be found after copying and pasting the package in IDEA? Is there any solution?Why can't the main class be found after copying and pasting the package in IDEA? Is there any solution?Apr 19, 2025 pm 07:57 PM

Why can't the main class be found after copying and pasting the package in IDEA? Using IntelliJIDEA...

Java multi-interface call: How to ensure that interface A is executed before interface B is executed?Java multi-interface call: How to ensure that interface A is executed before interface B is executed?Apr 19, 2025 pm 07:54 PM

State synchronization between Java multi-interface calls: How to ensure that interface A is called after it is executed? In Java development, you often encounter multiple calls...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.