Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Roman Numeral generator. You must ask the user for a 3 digit number and

ID: 3869680 • Letter: C

Question

Create a Roman Numeral generator. You must ask the user for a 3 digit number and then output the correct corresponding Roman Numeral. An example output would look like this:

Enter a 3 digit number: 214

215 as a Roman Numeral is: CCXIV

Cannot use loops.

Please switch or if/else.

Roman Numeral symbols needed to solve this problem.

Number

Roman Numeral

1

I

4

IV

5

V

9

IX

10

X

40

XL

50

L

90

XC

100

C

400

CD

500

D

900

CM

Number

Roman Numeral

1

I

4

IV

5

V

9

IX

10

X

40

XL

50

L

90

XC

100

C

400

CD

500

D

900

CM

Explanation / Answer

//This code is written in Java language.

import java.util.*;
import java.lang.*;
import java.io.*;

class RomanNumeralGenerator{
   //Creating map to store the mapping
   private final static TreeMap<Integer, String> map = new TreeMap<Integer, String>();

   static {
            map.put(1, "I");
            map.put(4, "IV");
            map.put(5, "V");
            map.put(9, "IX");
            map.put(10, "X");
            map.put(40, "XL");
            map.put(50, "L");
            map.put(90, "XC");
            map.put(100, "C");
            map.put(400, "CD");
            map.put(500, "D");
            map.put(900, "CM");
    }

    public static String ConvertToRoman(int number) {
       int l = map.floorKey(number);
   if ( number == l ) {
               return map.get(number);
   }
   return map.get(l) + ConvertToRoman(number-l); //Recursive call
    }
    //Main Method
    public static void main (String[] args) throws java.lang.Exception
    {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter a 3 digit number : ");
       int num=sc.nextInt();
       if(num<1000 && num>0)
       System.out.println(num + " as a Roman Numeral is : "+ RomanNumeralGenerator.ConvertToRoman(num));
   else
       System.out.println("Entered number is not in the range.");
    }
}