Write a program that converts a number entered in Roman numerals to decimal. You
ID: 3676105 • Letter: W
Question
Write a program that converts a number entered in Roman numerals to decimal. Your program shoild consist of a class, say Roman. An object should do the following
-Store the number as a Roman numeral
-convert and store the numeral as a decimalthe
-print the number as a Roman numeral or decimal 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
-your class must contain the method romanToDecimal to convert a roman numeral into its equivalent decimal number
-test your program using: MCXIV, CCCLIX, and MCDLXVI
Explanation / Answer
import java.util.Scanner;
import java.io.*;
class Roman
{
int decimalno;
static char convertToDecimal;
public static void main(String args[])
{
roman a=new roman();
a.convertToDecimal(decimalno);
printRoman(romannumerical);
printDecimal(decimalNum);
}
public void convertToDecimal()
for converting roman to decimal
{
Scanner scn=new Scanner(System.in);
//creating a scanner function
int num=0;
int l=romannumerical.toUpperCase;
do
{
switch(convertToDecimal)
//given decimal values of the roman numerals
{
case ‘M’;
decimal +=1000;
break;
case ‘D’;
decimal +=500;
break;
case ‘C’;
decimal +=100;
break;
case ‘L’;
decimal +=50;
break;
case ‘X’;
decimal +=10;
break;
case ‘V’;
decimal +=5;
break;
case ‘I’;
decimal +=1;
break;
}
num++;
if(num<romannumerical.length());
//roman no length
{
decimalno=decimalno-no;
}
public static void printDecimal(int decimalno)
{
System.out.println(“Decimal number stored is”);
}
return 0;
}
Total values:-
MCDLXVI=1666
MCXIV=1116
CCCLIX=361
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.