SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Decimal to Roman Conversion Program Method 1 in Java and Python

25 October 2012

Decimal to Roman conversion method 1 using place-value 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.

Roman numerals follow place-value combinations such as M for 1000, CM for 900, D for 500, CD for 400, C for 100, XC for 90, L for 50, XL for 40, X for 10, IX for 9, V for 5, IV for 4 and I for 1.

INPUT: Enter a Number: 3482 OUTPUT: Roman Equivalent = MMMCDLXXXII

Algorithm:

Step 1: Start.

Step 2: Accept decimal number num.

Step 3: Check whether num lies between 1 and 3999.

Step 4: If it is outside the range, display an error message and stop.

Step 5: Store Roman equivalents for thousands, hundreds, tens and units in four string arrays.

Step 6: Calculate the thousand digit using num / 1000.

Step 7: Calculate the hundred digit using (num / 100) % 10.

Step 8: Calculate the ten digit using (num / 10) % 10.

Step 9: Calculate the unit digit using num % 10.

Step 10: Use these digits as indexes in the four Roman arrays and join the strings.

Step 11: Display the Roman equivalent.

Step 12: Stop.

Explanation:

This method converts the decimal number to Roman form by separating the number into place values. The program first checks whether the number lies in the valid range 1 to 3999, because the arrays used here are prepared only for the usual Roman representation up to 3999.

Four arrays are used: thou, hund, ten and unit. Each array stores the Roman equivalent for one particular place. This is important because the same digit has a different Roman form depending on its place. For example, 4 in the units place is IV, 4 in the tens place is XL, and 4 in the hundreds place is CD.

The program extracts the thousands, hundreds, tens and units digits using division and modulus operations. The variable th stores the thousands digit, h stores the hundreds digit, t stores the tens digit, and u stores the units digit. Each extracted digit is then used as an index in its respective array. Finally, the four Roman parts are joined in the order thousands, hundreds, tens and units to form the complete Roman numeral.

This approach is especially easy for students because it avoids complex repeated subtraction. It depends on the observation that Roman notation for each place can be prepared in advance. Once the thousands, hundreds, tens and units digits are known, the conversion becomes a direct selection problem. The program therefore separates two tasks clearly: first extract the decimal digits, then use those digits as positions in the Roman arrays. This also makes special Roman forms such as IV, IX, XL and CM simple, because they are already stored in the correct arrays.

Java Program:

Java
/**
* The class Dec2Roman_Method1 takes a Decimal number as Input and finds its Roman Equivalent.
* This is Method 1
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class Dec2Roman_Method1
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a Number: ");
        int num = sc.nextInt();

        if(num > 0 && num < 4000)
        {
            // Roman equivalents for each place value.
            String thou[] = {"", "M", "MM", "MMM"};
            String hund[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
            String ten[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
            String unit[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

            // Extract digits from each place.
            int th = num / 1000;
            int h = (num / 100) % 10;
            int t = (num / 10) % 10;
            int u = num % 10;

            System.out.println("Roman Equivalent = " + thou[th] + hund[h] + ten[t] + unit[u]);
        }
        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:

Python
n = int(input("Enter a number: "))

thou = ["", "M", "MM", "MMM"]
hund = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
ten = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
unit = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]

# Each digit is used as an index in its own place-value list.
thousands = n // 1000
hundreds = (n // 100) % 10
tens = (n // 10) % 10
ones = n % 10

# The extracted digits now select the correct Roman part for each place.
# Join the Roman symbols from highest place value to lowest.
roman = thou[thousands] + hund[hundreds] + ten[tens] + unit[ones]
print("Roman Number =", roman)

Output:

Enter a Number: 3482 Roman Equivalent = MMMCDLXXXII

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →