In: Computer Science
Write a simple java program to list roman numeral for a given range of numbers.
Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M.
I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000
Roman numerals are usually written largest to smallest from left to right. But a number
like 4 is not written as IIII. It is written as IV.
Because the one is before the five, we subtract it making four.
The same principle applies to the number nine, which is written as IX.
There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
To DO:
Given a range of values, convert all integers within the range (inclusive) to roman
numerals, printing them out with one roman numeral per line.
Convert all integers in the range from 1 to 3999 to roman numerals and them out with
one roman numeral per line.
Example of input/output:
enter two positive integers, smaller followed by larger:
2 6
Output should be roman numeral for all the integers in the range as:
II
III
IV
V
VI
Approach:
1) Store the roman values of thousands , hundreds, tens, ones into arrays , so that we can use these arrays when digit places are placed in different places
2) Divide the decimal number into digits (thousands , hundreds , tens , ones)
3)Print the corresponding roman value for all the digits in the decimal number
Code:
import java.util.Scanner; // scanner class for standard
i/o
class Main
{
static String decimalToRomanNumeral(int num) // method to convert
the decimal numbe roman numeral
{
// we strore the roman values of the digits from 0 to 9
// these arrays helpul when they are placed in different
places
String thousand[] = {"", "M", "MM", "MMM"};
String hundred[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC",
"DCCC", "CM"};
String ten[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX",
"LXXX", "XC"};
String one[] = {"", "I", "II", "III", "IV", "V", "VI", "VII",
"VIII", "IX"};
// we will divide the decimal number into digits like thousands,
hundreds , tens , ones
// from thousands place print the corresponding roman value stoed
in the string array m
// like wise repeat for all digits
// finally combine all the roman numeral strings to get the final
roman numeral
String thousands = thousand[num/1000];
String hundereds = hundred[(num%1000)/100];
String tens = ten[(num%100)/10];
String ones = one[num%10];
String result = thousands + hundereds + tens + ones; // final roman
numeral
return result; // returns result
}
public static void main(String []args) { // main method
Scanner sc = new Scanner(System.in); // scanner class
System.out.println("Enter two positive integers, smaller followed
by larger:");
int n1 = sc.nextInt(); // user input n1
int n2 = sc.nextInt(); // user input n2
for(int i=n1;i<=n2;i++)
System.out.println(decimalToRomanNumeral(i));
//for(int i=1;i<=3999;i++)
//System.out.println(decimalToRomanNumeral(i));
}
}
Output:
Output for roman numerals between 1 and 3999 Some outputs i have attached here...