Question: Write a Java program that takes a Roman numeral character as input (I, V, X, L, C, D, or M) and prints an integer according to the following rules:
Question; Write a Java program that takes a Roman numeral character as input (I, V, X, L, C, D, or M) and prints an integer according to the following rules:
Ans:
package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main(String[] args) {
// Question: Roman Numeral Converter
//
// Write a Java program that takes a Roman numeral character as input (I, V, X, L, C, D, or M) and prints an integer according to the following rules:
//
// I is equivalent to 1
// V is equivalent to 5
// X is equivalent to 10
// L is equivalent to 50
// C is equivalent to 100
// D is equivalent to 500
// M is equivalent to 1000
// If the input character is not a valid Roman numeral, print -1.
Scanner scan = new Scanner(System.in);
char ch = scan.next().charAt(0);
if (ch == 'I') {
System.out.print("1");
}
else if (ch == 'V'){
System.out.print("5");
}
else if (ch == 'X') {
System.out.print("10");
}
else if (ch == 'L') {
System.out.print("50");
}
else if (ch == 'C') {
System.out.print("100");
}
else if (ch == 'D') {
System.out.print("500");
}
else if (ch == 'M') {
System.out.print("1000");
}
else {
System.out.print("-1");
}
}
}
Comments
Post a Comment