What are Roman numbers
Roman numbers are used by Romans where numbers are represented by English letters. Each Roman symbol represents an equivalent decimal number.
There are certain rules which need to be followed while writing Roman numbers which are explained next.
Roman symbol chart is given below.
Roman Number | I | V | X | L | C | D | M |
Decimal equivalent | 1 | 5 | 10 | 50 | 100 | 500 | 1000 |
Rules for Roman Number Representation
- A Roman digit can be repeated at most 3 times. Thus, IIII, VVVV, XXXX are all invalid.
- A Roman digit when placed to the left of another Roman digit is subtracted from the right digit. Thus, IV means 5 – 1 = 4.
- A Roman digit placed to the right of another Roman digit is added to the right digit. Thus, LX means 50 + 10 = 60.
Program to convert Decimal number to Roman
This post provides two methods to convert a decimal number into its Roman equivalent.
Both the programs can only convert numbers in the range 1 – 3999.
Method 1 : Using Multiple Arrays
This method creates arrays for units, tens, hundreds and thousands and initialized them with all the possible values in these categories.
It reads the decimal number to be converted and extracts each of its units, tens, hundreds and thousand digits from it using division(/) and modulus(%) operators and retrieves their equivalents from Roman digit arrays.
Roman values retrieved from the arrays are merged to form the resultant number.
import java.util.Scanner;
public class RomanNumberConverter {
public static void main(String[] args) {
// initialize reader for reading decimal number
Scanner reader = new Scanner(System.in);
System.out.println("Enter a number");
// read number
int number = reader.nextInt();
methodOne(number);
// close reader
reader.close();
}
static void methodOne(int number) {
// declare arrays for Roman numbers
String[] thousands = { "", "M", "MM", "MMM" };
String[] hundreds = { "", "C", "CC", "CCC", "CD", "D",
"DC", "DCC", "DCCC", "CM" };
String[] tens = { "", "X", "XX", "XXX", "XL", "L",
"LX", "LXX", "LXXX", "XC" };
String[] units = { "", "I", "II", "III", "IV", "V", "VI",
"VII", "VIII", "IX", "X" };
// get thousands in the decimal number
int numberOfThousands = number / 1000;
// get hundreds in the decimal number
int numberOfHundreds = (number / 100) % 10;
// get tens in the decimal number
int numberOfTens = (number / 10) % 10;
// get units in the decimal number
int numberOfUnits = number % 10;
// get the corresponding Roman digits and merge them
String romanNumber = thousands[numberOfThousands] + hundreds[numberOfHundreds]
+ tens[numberOfTens] + units[numberOfUnits];
System.out.println("Roman equivalent of " + number + " is " + romanNumber);
}
}
Method 2 : Using Arrays of unique numbers
This method first initializes a couple of arrays. One array is of unique Roman numbers.
Unique numbers means combination of numbers such that no Roman numeral is repeated.
Examples of unique combination of Roman numerals are I, IV, X, XL, LX etc.
Second array contains equivalent decimal digits of Roman numerals.
Now a loop is initiated for the total count of size of arrays(which is 13). Inside this loop, a while
loop is initiated. This loop determines the number of units in the number to be converted by subtracting the decimal digit at the current array index from the number.
Example,
Suppose the number is 1250, while
loop checks if the number > 1000.
If it is greater, the loop extracts the corresponding Roman numeral at the current index from the Roman numeral array, concatenates it to the result string and then subtracts the current decimal array digit(1000) from the number.
It checks again, now if the number is < 1000, it is checked for the next decimal array element(that is, 900).
If the number is > 100, again the same procedure is followed else the number is checked against the next decimal array element(that is, 500).
import java.util.Scanner;
public class RomanNumberConverter {
public static void main(String[] args) {
// initialize reader for reading decimal number
Scanner reader = new Scanner(System.in);
System.out.println("Enter a number");
// read number
int number = reader.nextInt();
// check for range
if (number > 0 && number < 4000) {
methodTwo(number);
} else {
System.out.println("Please enter a number in the range less than 4000");
}
// close reader
reader.close();
}
static void methodTwo(int number) {
// create arrays of unique digits
String roman[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL",
"X", "IX", "V", "IV", "I" };
int decimal[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
String romanNumber = "";
// iterate over the array
for (int i = 0; i < 13; i++) {
// iterate till the number is less than decimal array value
while (number >= decimal[i]) {
// extract the corresponding Roman number
romanNumber = romanNumber + roman[i];
// reduce the number
number = number - decimal[i];
}
}
System.out.println("Roman Equivalent = " + romanNumber);
}
}
Let’s tweak in
- For converting a number greater than 4000 to Roman, a separate notation is used in which a bar is applied over the Roman numeral.