Tag Archives: roman

Converting an integer into roman numerals in Java

Roman numerals are a system of numerical notations used by the Romans. In this blog post I will be discussing a method to convert an integer into it’s equivalent Roman numeral representation. In Roman number system letter of Latin are used to represent the number. The letters and their corresponding values are as follows:

I: 1
V: 5
X: 10
L: 50
C: 100
D: 500
M: 1000

Numbers are formed by combining symbols together and adding the values. For example 1 is I, 12 is XII. However, in those cases when four successive character are coming to be same subtractive notation is used. For example 9 us not written as VIIII but as IX, 40 is not written as XXXX but as XL. Students are advised to visit the Wikipedia Page for Roman Numerals for details. There is no symbol for zero in Roman numeral system.

The problem which ICSE/ISC students usually face when writing a program to convert a positive integer into Roman numerals is how to handle the subtractive cases i.e. numbers like IX (9), XL(40), XC (90) etc. The first thought which comes to mind is to use a conditional statement but on closer inspection it turn out that it’s out that too many if statements would be required. It’s possible to solve the problem by using multiple if statement but there exists an elegant  method also and that is to include subtractive cases also as follows:

I: 1
IV: 4
V: 5
IX: 9
X: 10
XL: 40
L: 50
XC: 90
C: 100
CD: 400
D: 500
CM: 900
M: 1000

In the above list we have included the subtractive cases viz.- 9, 40, 90, 400 and 900. Now, the logic is we have to divide out number by the largest factor first (1000), the corresponding symbol will be repeated quotient times, the remainder will then become the number for future division and repetitions. The process will be repeated until the number becomes zero. For example- if the number is 2015, we will divide it by 1000, the quotient and remainder would be 2 and 15 respectively. The corresponding symbol M will be repeated twice. The remainder 15 will now be completely divisible by 10, the  quotient and remainder would be 1 and 5 respectively. The corresponding symbol X will be added to the answer which will now become MMX. The remainder 5 will now be completely divisible by 5, the  quotient and remainder would be 1 and 0 respectively. The corresponding symbol X will be added to the answer which will now become MMXV. The remainder is now 0 and therefore we will stop. A primitive version using the above logic is as follows:

class Roman{
    public static String IntegerToRoman(int n){
        String roman="";
        int repeat;

        repeat=n/1000;
        for(int i=1; i<=repeat;i++){
            roman=roman+"M";
        }
        n=n%1000;

        repeat=n/900;
        for(int i=1; i<=repeat;i++){
            roman=roman+"CM";
        }
        n=n%900;

        repeat=n/500;
        for(int i=1; i<=repeat;i++){
            roman=roman+"D";
        }
        n=n%500;

        repeat=n/400;
        for(int i=1; i<=repeat;i++){
            roman=roman+"CD";
        }
        n=n%400;

        repeat=n/100;
        for(int i=1; i<=repeat;i++){
            roman=roman+"C";
        }
        n=n%100;

        repeat=n/90;
        for(int i=1; i<=repeat;i++){
            roman=roman+"XC";
        }
        n=n%90;

        repeat=n/50;
        for(int i=1; i<=repeat;i++){
            roman=roman+"L";
        }
        n=n%50;

        repeat=n/40;
        for(int i=1; i<=repeat;i++){
            roman=roman+"XL";
        }
        n=n%40;

        repeat=n/10;
        for(int i=1; i<=repeat;i++){
            roman=roman+"X";
        }
        n=n%10;

        repeat=n/9;
        for(int i=1; i<=repeat;i++){
            roman=roman+"IX";
        }
        n=n%9;

        repeat=n/5;
        for(int i=1; i<=repeat;i++){
            roman=roman+"V";
        }
        n=n%5;

        repeat=n/4; 
        for(int i=1; i<=repeat;i++){
            roman=roman+"IV";
        }
        n=n%4;

        repeat=n/1; // or simply repeat=n or i<=n in the condition part of the loop
        for(int i=1; i<=repeat;i++){
            roman=roman+"I";
        }
        return roman;
    }
    public static void main(String args[]){
        System.out.println("12: "+IntegerToRoman(12));
        System.out.println("999: "+IntegerToRoman(999));
    }
}

Students of class 10 and above should notice that the size of the program can be drastically reduced if we use an array to store the symbols and magnitudes. The array version using the same logic is as follows:

class Roman{
    public static String IntegerToRoman(int n){
        String roman="";
        int repeat;
        int magnitude[]={1000,900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String symbol[]={"M","CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        
        for(int x=0; x<magnitude.length; x++){
            repeat=n/magnitude[x];
            for(int i=1; i<=repeat; i++){
                roman=roman + symbol[x];
            }
            n=n%magnitude[x];
        }
        return roman;
    }

    public static void main(String args[]){
        System.out.println("12: "+IntegerToRoman(12));
        System.out.println("999: "+IntegerToRoman(999));
    }
}

As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.

An easy way to check the answer is to type the following in the Google search bar/page:

12 in roman

🙂