Convert Roman numerals to decimal using Java
###### Posted by Samath
Last Updated: January 17, 2024

Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class, say, Roman. An object of type Roman should do the following:

a. Store the number as a Roman numeral.

b. Convert and store the number into decimal.

c. Print the number as a Roman numeral or decimal number as requested by the user.

The decimal values of the Roman numerals are:
M 1000
D 500
C 100
L 50
X 10
V 5
I 1

d. You should be able to enter data into your program using the following Roman numerals : MCXIV, CCCLIX, and MDCLXVI which should be able to produce outputs as shown below

Enter a roman number: MCXIV
The equivalent of the Roman numeral MCXIV is 1114

Enter a roman number: CCCLIX
The equivalent of the Roman numeral CCCLIX is 359

Enter a roman number: MDCLXVI
The equivalent of the Roman numeral MDCLXVI is 1666

Solution:

```import java.util.Scanner;

public class Roman {
static String romanNumeral;
static int decimalNum;
public static void main(String args[]) {
Roman rmtoD = new Roman();
rmtoD .convertToDecimal();
rmtoD .printRoman(romanNumeral);
}

public void convertToDecimal () {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Roman number: ");
romanNumeral = scan.nextLine();
romanNumeral = romanNumeral.toUpperCase();

int l=  romanNumeral.length();
int num=0;
int previousnum = 0;
for (int i=l-1;i>=0;i--)
{
char x =  romanNumeral.charAt(i);
x = Character.toUpperCase(x);
switch(x)
{
case 'I':
previousnum = num;
num = 1;
break;
case 'V':
previousnum = num;
num = 5;
break;
case 'X':
previousnum = num;
num = 10;
break;
case 'L':
previousnum = num;
num = 50;
break;
case 'C':
previousnum = num;
num = 100;
break;
case 'D':
previousnum = num;
num = 500;
break;
case 'M':
previousnum = num;
num = 1000;
break;
}
if (num<previousnum)
{decimalNum= decimalNum-num;}
else
decimalNum= decimalNum+num;
}
}
public static void printRoman (String romanNumeral){
System.out.println ("The equivalent of the Roman numeral "+romanNumeral+" is "+decimalNum);
}

public static void printDecimal (int decimalNum){
System.out.println ("Decimal Number stored is: " + decimalNum);
}

}```

###### Convert Integer to Roman Numeral using C#
Samath | Jan 20, 2024
###### C++ Convert an Integer to Roman Numeral
Samath | Jan 20, 2024
###### Python Convert Decimal to Binary or Binary to Decimal
Samath | Jan 09, 2017
###### C++ Convert from Decimal to Octal
Samath | Jan 05, 2017
###### Python to Convert Decimal to Binary, Octal and Hexadecimal
gideonna | Apr 13, 2016
###### Decimal to Binary in C++
Samath | Dec 26, 2014
###### SQL Display two digits after decimal point
SceDev | Feb 08, 2024
###### Converting Time To Decimal using C++
Samath | Jan 04, 2017
###### Binary to Decimal Conversion in C#
Samath | Jan 10, 2021
###### C++ program that prompts the user to input a decimal number and outputs the number rounded to the nearest integer
Samath | Apr 06, 2022
###### C++ program that prompts the user to input five decimal numbers, prints the five decimal numbers, converts each decimal number to the nearest integer, adds the five integers and prints the sum and average of the five integers.
Samath | Apr 06, 2022
###### JavaScript program that convert a string to spinal case
SceDev | Mar 28, 2024
###### How do you use the LOWER function to convert a string to lowercase?
LeoRobs | Jun 08, 2024
###### How do you use the TRY_CONVERT function to convert data types with error handling?
LeoRobs | Jun 29, 2024