1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Gre4nikov [31]
4 years ago
12

Write a program that converts a number entered in Roman numerals to decimal.

Computers and Technology
1 answer:
Montano1993 [528]4 years ago
5 0

Answer:

Here is the JAVA program:

import java.util.Scanner;   // for importing scanner class

public class RomanToDecimal{  //class to convert roman numeral to decimal

       static String romNum;  // variable that stores roman numeral

       static int decNum;  //variable that stores decimal number

       public static void main(String args[]) {  //start of the program

              //romtodec object created

              RomanToDecimal romtodec = new RomanToDecimal();  

//call function decimalConversion() that converts roman numeral to decimal

              romtodec. decimalConversion();  

//call function displayRomanNumeral to display the results of conversion

               romtodec. displayRomanNumeral(romNum);

//call function to display the decimal no. of roman numeral

               romtodec . displayDecimalNumber(decNum);   }

//method decimalConversion to convert Roman numeral to decimal

     public void decimalConversion () {

     Scanner scan = new Scanner(System.in);  //to get input

       //prompts user to enter a Roman numeral

     System.out.print("enter a Roman numeral: ");

               romNum = scan.nextLine();  //reads input    

               //stores the length of the entered Roman numeral                                            

                int length=  romNum.length();  

               int number=0;  //initializes number to 0

               int prevNum = 0;  //initializes previous number to 0

          //loops through the length of the input Roman numeral

               for (int i=length-1;i>=0;i--)  

//charAt() method returns the roman numeral symbol at the index i

               { char symbol =  romNum.charAt(i);

       //every symbol will be converted to upper case

                       symbol = Character.toUpperCase(symbol);

//switch statement tests the symbol variable for equality against a list of //numerals given and each numeral is assigned its equal decimal value.

                         switch(symbol)

                       {   case 'I':

                               prevNum = number;

                               number = 1;

                               break;

                            case 'V':

                                    prevNum = number;

                               number = 5;

                               break;

                               case 'X':

                                       prevNum = number;

                               number = 10;

                                break;

                               case 'L':

                                       prevNum = number;

                               number = 50;

                               break;

                               case 'C':

                                        prevNum = number;

                               number = 100;

                               break;

                               case 'D':

                                        prevNum = number;

                               number = 500;

                               break;

                               case 'M':

                                        prevNum = number;

                               number = 1000;

                               break; }          

/*checks if the number is greater than previous number

if the value of current roman symbol (character) is greater than the next

symbol's value. then add value of current roman symbol to the decNum

else subtract the value of current roman symbol from the value of its next symbol. */              

                       if (number>prevNum)

                           { decNum= decNum+number;  }

                       else

                          decNum= decNum-number;  }

//displays the decimal value of the roman numeral

       public static void displayRomanNumeral (String romNum){

               System.out.println ("The Roman numeral "+romNum+" is equal to decimal number"+decNum); }

//displays the number as decimal

           public static void displayDecimalNumber (int decNum){

       System.out.println ("The decimal number is: " + decNum);  } }      

Output:

enter a Roman numeral: MCXIV

The Roman numeral MCXIV is equal to decimal number 1114

Output:

enter a Roman numeral: CCCLIX

The Roman numeral CCCLIX is equal to decimal number 359

Output:

enter a Roman numeral: MDCLXVI

The Roman numeral MDCLXVI is equal to decimal number 1666

You might be interested in
What is a difference between a waxing crescent and a waning gibbous? (1 point) waxing crescent: first quarter waning gibbous: th
iren2701 [21]

Answer:

  waxing crescent: first quarter waning gibbous: third quarter

Explanation:

The moon is waxing (getting larger) in the first quarter after the new moon. It is waning (getting smaller) in the third quarter, after the full moon at the end of the second quarter.

The shape of the moon is a "crescent" when it is less than a semicircle. It is "gibbous" when it is more than a semicircle. As it waxes, the crescent grows until the moon is half-full at the end of the first quarter, then it is a waxing gibbous moon through the second quarter until it is full at the end of the quarter. Following the full moon, it is waning gibbous until it becomes half-full again at the end of the third quarter. Finally, it is a waning crescent through the fourth quarter until the new moon.

  waxing crescent: first quarter waning gibbous: third quarter

5 0
3 years ago
Your classmate is frustrated because the code that was designed to add up her five quiz grades is not working properly.
inna [77]

Answer:

sum = 0 must be before for loop. If inside for loop, it will keep resetting sum to 0 each iteration.

Explanation:

hope this helps

7 0
3 years ago
____ is a method of querying and reporting that takes data from standard relational databases, calculates and summarizes the dat
IgorLugansk [536]

Answer: Online analytical processing

Explanation:

Online analytical processing(OLAP) is the method through which multi-dimension analytical queries can be resolved.User can attain selected query data as per their selection and then assess and analyze them from different view point.

  • It helps in viewing and reporting , planning ,performing analytical calculation, collecting data in data cubes etc.
  • Other options are incorrect because data adjacency, Ad hoc reporting ,data segregation and E-discovery are not the mechanism through which query and storage of data in data cubes take place.
  • Thus, the correct option is online analytical processing
3 0
3 years ago
How to disable Fortinet?
Anni [7]

Answer:

Click on the start menu and go to the control panel. Video of the Day.

Click "Programs and Features" to launch the programs and features window.

Scroll down the window, click "Fortinet Antivirus," and then click the uninstall button.

Explanation:

8 0
2 years ago
Should charter schools allow cell phone use in class?
lawyer [7]

Answer:

yes

Explanation:

4 0
4 years ago
Read 2 more answers
Other questions:
  • True or False. It is required to restart the client computer after the DLP agent has been installed.
    9·2 answers
  • You are holding a rock tied to a string, which is an example of a pendulum. identify and explain the proper term for each part o
    10·2 answers
  • When should students practice netiquette in an online course?
    9·1 answer
  • A ____ appears at the bottom of your e-mail messages and contains standard information about yourself that the recipient can use
    5·1 answer
  • Which button would you use to insert a downloaded video clip on your computer into a slide?
    15·2 answers
  • Is it important for a writer to include voice and point of view in writing because...<br> Plz help
    10·1 answer
  • Sean Cody is a website most known for what?
    9·2 answers
  • A program that doesn’t work properly needs to be debugged. true or false
    8·1 answer
  • Suppose we want to select between two prediction models M1 and M2. We have performed 10-fold cross validation on each. The error
    14·1 answer
  • How does social network use message to entertain?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!