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.
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);
}
}
Comments
Leave a comment