Decimal to Roman Conversion Program Method 2 in Java and Python
Decimal to Roman conversion method 2 using value-symbol arrays, with algorithm, explanation, Java solution and Python solution.
Question:
Write a program to find the Roman equivalent of any decimal number entered by the user. The number entered should be in the range 1 to 3999.
This method uses arrays of Roman symbols and their decimal values, then repeatedly subtracts the largest possible value.
Algorithm:
Step 1: Start.
Step 2: Accept decimal number num.
Step 3: Store Roman symbols in array roman.
Step 4: Store corresponding decimal values in array decimal.
Step 5: Check whether num lies between 1 and 3999.
Step 6: If it is outside the range, display an error message and stop.
Step 7: Initialize result string str as blank.
Step 8: Run a loop through all indexes of the decimal array.
Step 9: While num is greater than or equal to decimal[i], subtract decimal[i] from num.
Step 10: Each time subtraction is done, append roman[i] to result string.
Step 11: Display the final Roman string.
Step 12: Stop.
Explanation:
This method uses two parallel arrays to convert a decimal number to Roman notation. The array roman stores Roman symbols, and the array decimal stores their corresponding values. Since both arrays are parallel, roman[i] represents the value stored in decimal[i].
The arrays are arranged from the largest value to the smallest value. This order is important because Roman numerals are formed by using the largest possible symbol first. For example, while converting 3482, the program first tries 1000 and adds M as many times as possible. Then it moves to 900, 500, 400 and so on.
The outer for loop selects each Roman value in decreasing order. The inner while loop is used because the same Roman symbol may be needed more than once. Whenever the remaining number is greater than or equal to decimal[i], that value is subtracted and the matching Roman symbol is added to the result. Subtractive cases such as CM, CD, XC, XL, IX and IV are already included in the arrays, so the loop naturally handles them without extra conditions.
This method is more general in style than the place-value array method because it builds the Roman numeral by consuming the number from left to right in terms of value. The remaining value of the number keeps decreasing. Whenever a Roman symbol is used, its corresponding decimal value is removed from the number. Including subtractive pairs such as CM, CD, XC, XL, IX and IV in the arrays prevents wrong forms like DCCCC or IIII. Thus the array order controls the correctness of the final Roman representation.
Java Program:
/**
* The class Dec2Roman_Method2 takes a Decimal number as Input and finds its Roman Equivalent.
* This is Method 2
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class Dec2Roman_Method2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str = "";
System.out.print("Enter a Number: ");
int num = sc.nextInt();
// Roman symbols are arranged from the highest value to the lowest.
String roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
// decimal[i] is the value of roman[i].
int decimal[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
if(num > 0 && num < 4000)
{
// Check each Roman value, beginning with the largest possible value.
for(int i = 0; i < 13; i++)
{
// Keep subtracting the current value while it can be used.
while(num >= decimal[i])
{
num = num - decimal[i];
str = str + roman[i];
}
}
System.out.println("Roman Equivalent = " + str);
}
else
{
System.out.println("\nYou entered a number out of Range.");
System.out.println("Please enter a number in the range [1-3999]");
}
}
}Equivalent Python Program:
n = int(input("Enter a number: "))
roman = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
result = ""
# The two lists are parallel, so roman[i] represents decimal[i].
# Move from the largest Roman value to the smallest.
for i in range(len(decimal)):
# Use the same symbol as long as its decimal value can be subtracted.
while n >= decimal[i]:
result = result + roman[i]
n = n - decimal[i]
print("Roman Number =", result)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.