Roman Numerals in Java

Sheeraz Gul Oct 12, 2023
Roman Numerals in Java

This tutorial demonstrates how to convert integers to roman numerals in Java.

Roman Numerals in Java

Converting the integers to roman numerals is frequently required while working in Java. Most of the time, this question is asked during interviews with the TOP IT companies.

Roman numerals are the character representation of numbers; we can usually find these numbers in watches or music theory. There are a total of seven letters that are used to represent a number in roman characters; see the table below:

Roman Numbers
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

The roman numerals are usually written from left to right and highest to lowest. The above table shows that we can not use a roman numeral more than three times; for example, we will write 4 like IV but not IIII.

Now let’s try to create an approach in Java to convert integers to roman numerals. Follow the steps below:

  • First, we have to create two arrays, one for the roman numeral and one for integers.
  • Then, create an instance of the string builder class.
  • The next step is to compare the integer with roman numerals.
  • If the integer is >=, then the highest roman numeral, we need to add it to the string builder instance and reduce its value from the input number.
  • If the integer is <, then the highest roman numeral, we need to check to the highest roman numeral and repeat the process until the input number is 0.
  • The final string builder will be the converted roman numeral.

Now let’s try to implement a Java program based on the above steps:

package delftstack;

public class Example {
  public static void IntegerToRoman(int DemoInteger) {
    System.out.println("The Integer Number is : " + DemoInteger);
    int[] IntegerValues = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    String[] RomanValues = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    StringBuilder RomanNumber = new StringBuilder();
    for (int x = 0; x < IntegerValues.length; x++) {
      while (DemoInteger >= IntegerValues[x]) {
        DemoInteger = DemoInteger - IntegerValues[x];
        RomanNumber.append(RomanValues[x]);
      }
    }
    System.out.println("The Roman Numeral for given number is: " + RomanNumber.toString());
  }
  public static void main(String args[]) {
    IntegerToRoman(121);
    IntegerToRoman(280);
    IntegerToRoman(999);
    IntegerToRoman(1000);
    IntegerToRoman(1100);
  }
}

The code above will convert the given integer to roman numerals. See the output:

The Integer Number is : 121
The Roman Numeral for given number is: CXXI
The Integer Number is : 280
The Roman Numeral for given number is: CCLXXX
The Integer Number is : 999
The Roman Numeral for given number is: CMXCIX
The Integer Number is : 1000
The Roman Numeral for given number is: M
The Integer Number is : 1100
The Roman Numeral for given number is: MC

This is not the only way to convert the integers to roman numerals. We can also create four arrays instead of two; these four arrays will be for roman numerals in the form of units, tens, hundreds, and thousands; and then we can use these arrays to generate roman numerals for the given number using the same string builder method, see the example:

package delftstack;

public class Example {
  public static String IntegerToRoman(int DemoIntger) {
    String[] ThousandsRoman = {"", "M", "MM", "MMM"};
    String[] HundredsRoman = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
    String[] TensRoman = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    String[] UnitsRoman = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
    return ThousandsRoman[DemoIntger / 1000] + HundredsRoman[(DemoIntger % 1000) / 100]
        + TensRoman[(DemoIntger % 100) / 10] + UnitsRoman[DemoIntger % 10];
  }
  public static void main(String args[]) {
    int[] Integers = {
        17, 23, 48, 53, 181, 191, 147, 150, 105, 128, 199, 98, 212, 2181, 3118, 1100, 1111};
    for (int DemoInteger : Integers) {
      System.out.printf("Integer %4d -> Roman %8s\n", DemoInteger, IntegerToRoman(DemoInteger));
    }
  }
}

The code above will convert the given array of integers to the roman numerals one by one. See output:

Integer   17 -> Roman     XVII
Integer   23 -> Roman    XXIII
Integer   48 -> Roman   XLVIII
Integer   53 -> Roman     LIII
Integer  181 -> Roman   CLXXXI
Integer  191 -> Roman     CXCI
Integer  147 -> Roman   CXLVII
Integer  150 -> Roman       CL
Integer  105 -> Roman       CV
Integer  128 -> Roman  CXXVIII
Integer  199 -> Roman    CXCIX
Integer   98 -> Roman   XCVIII
Integer  212 -> Roman    CCXII
Integer 2181 -> Roman MMCLXXXI
Integer 3118 -> Roman MMMCXVIII
Integer 1100 -> Roman       MC
Integer 1111 -> Roman     MCXI

If you don’t want to use arrays in your code, then you can also use the Maps, where we put the value of each integer with its numerals and then use it to convert integers to roman numerals. See the example:

package delftstack;

import java.util.LinkedHashMap;
import java.util.Map;
public class Example {
  public static String ConvertedRomanNumerals(int DemoInteger) {
    LinkedHashMap<String, Integer> RomanNumeralsMap = new LinkedHashMap<String, Integer>();

    RomanNumeralsMap.put("M", 1000);
    RomanNumeralsMap.put("CM", 900);
    RomanNumeralsMap.put("D", 500);
    RomanNumeralsMap.put("CD", 400);
    RomanNumeralsMap.put("C", 100);
    RomanNumeralsMap.put("XC", 90);
    RomanNumeralsMap.put("L", 50);
    RomanNumeralsMap.put("XL", 40);
    RomanNumeralsMap.put("X", 10);
    RomanNumeralsMap.put("IX", 9);
    RomanNumeralsMap.put("V", 5);
    RomanNumeralsMap.put("IV", 4);
    RomanNumeralsMap.put("I", 1);

    String RomanResult = "";

    for (Map.Entry<String, Integer> MapEntry : RomanNumeralsMap.entrySet()) {
      int MatchedInteger = DemoInteger / MapEntry.getValue();
      RomanResult = RomanResult + RepeatProcess(MapEntry.getKey(), MatchedInteger);
      DemoInteger = DemoInteger % MapEntry.getValue();
    }
    return RomanResult;
  }
  public static String RepeatProcess(String Roman, int DemoInteger) {
    if (Roman == null) {
      return null;
    }
    final StringBuilder DemoRomanBuilder = new StringBuilder();
    for (int i = 0; i < DemoInteger; i++) {
      DemoRomanBuilder.append(Roman);
    }
    return DemoRomanBuilder.toString();
  }

  public static void main(String args[]) {
    for (int x = 1; x < 31; x++) {
      System.out.println("Integer = " + x + " -> Roman = " + ConvertedRomanNumerals(x));
    }
  }
}

The code above will use Map to convert the integer to roman numerals. This code will convert the integers from 1 to 30 to the roman numerals. See output:

Integer = 1 -> Roman = I
Integer = 2 -> Roman = II
Integer = 3 -> Roman = III
Integer = 4 -> Roman = IV
Integer = 5 -> Roman = V
Integer = 6 -> Roman = VI
Integer = 7 -> Roman = VII
Integer = 8 -> Roman = VIII
Integer = 9 -> Roman = IX
Integer = 10 -> Roman = X
Integer = 11 -> Roman = XI
Integer = 12 -> Roman = XII
Integer = 13 -> Roman = XIII
Integer = 14 -> Roman = XIV
Integer = 15 -> Roman = XV
Integer = 16 -> Roman = XVI
Integer = 17 -> Roman = XVII
Integer = 18 -> Roman = XVIII
Integer = 19 -> Roman = XIX
Integer = 20 -> Roman = XX
Integer = 21 -> Roman = XXI
Integer = 22 -> Roman = XXII
Integer = 23 -> Roman = XXIII
Integer = 24 -> Roman = XXIV
Integer = 25 -> Roman = XXV
Integer = 26 -> Roman = XXVI
Integer = 27 -> Roman = XXVII
Integer = 28 -> Roman = XXVIII
Integer = 29 -> Roman = XXIX
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook