Answer to Question #249683 in Java | JSP | JSF for cheese

Question #249683

write a java program that accepts an ordinary number and outputs its equivalent roman numerals. the ordinary numbers and their equivalent roman numerals are given below: ordinary numbers roman numerals.


1
Expert's answer
2021-10-12T00:37:25-0400
import java.util.Scanner;
public class Main {


    public static void intToRoman(int n) {


        System.out.println("Integer: " + n);
        int[] val = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
        String[] romanLiterals = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};


        StringBuilder roman = new StringBuilder();


        for(int i=0;i<val.length;i++) {
            while(n >= val[i]) {
                n -= val[i];
                roman.append(romanLiterals[i]);
            }
        }
        System.out.println("Roman: " + roman.toString());
    }


    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter an interger: ");
        int x=input.nextInt();
        System.out.println("converting to Roman number ");
        intToRoman(x);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS