# Convert Integer to Roman Numerals in Java

It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, and Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to discuss how to convert integer to roman in Java with different approaches and logic. Also, we will create Java programs for the same. ## Roman Numerals

Roman numerals are the symbolic representation of numbers. These are usually used in watch faces, music theory, etc. There are seven letters used to represent roman numerals. The following table represent the roman numerals and corresponding decimal values.

Character Roman Numeral
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

Roman numerals have the following properties:

It is usually written in highest to lowest from left to right, except some special cases (where the left character is less than the right character). For example, in roman numerals IV is equivalent to 4. It cannot be written as IIII.

In such a case, we subtract the subtract the left character value from the right character value. For example, IV will be 5-1=4. In the same way IX will be 10-1=9.

Consider the following cases:

• The roman numeral I can be placed before V or X, represents subtract one. For example, IV (5-1) = 4 and 9 is IX (10-1) = 9.
• The roman numeral X can be placed before L or C represents subtract ten. For example, XL (50-10) = 40 and XC (100-10) = 90.
• The roman numeral C placed before D or M represents subtract hundred. For example, CD (500-100) = 400 and CM (1000-100) = 900.

## Approach

An approach to convert integer to roman numeral is simple. First, create two arrays one for storing the values of roman numerals and second for storing the corresponding letters. Create an instance of the StringBuilder Class. Now compare the integer with roman numerals and do the following:

• If input number is >= highest roman numeral, add it to the string builder and reduce its corresponding value from the input number.
• If input number is < highest roman numeral, then check with next highest roman numerals. Repeat the process above till the input number becomes 0.

The StringBuilder will be the corresponding roman numeral.

Let's understand the above steps through an example.

Example

Suppose, we have to convert 36 into roman numerals. Compare the integer with roman values as follows.

1000>36 = yes, check with next roman numeral.

900>36 = yes, check with next roman numeral.

500>36 = yes, check with next roman numeral.

400>36 = yes, check with next roman numeral.

100>36 = yes, check with next roman numeral.

90>36 = yes, check with next roman numeral.

50>36 = yes, check with next roman numeral.

40>36 = yes, check with next roman numeral.

10<36, add corresponding literal 'X' to result, result =X, N = 36-10=26

10<26, add corresponding literal 'X' to result, result =XX, N = 26-10=16

10<16, add corresponding literal 'X' to result, result =XXX, N = 16-10=6

10>6, check with next roman numeral, result =XXX

9>6, check with next roman numeral, result =XXX

5<6, add corresponding literal 'V' to result, result =XXXV, N = 6-5=1

5>1, check with next roman numeral, result =XXXV

4>1, check with next roman numeral, result =XXXV

1==1, add corresponding literal 'I' to result, result =XXXVI, N = 1-1=0

Result = XXXVI

## Java Program to Convert Integer to Roman Numerals

IntegerToRoaman.java

Output: Another way to print roman numeral is to create four arrays of place values. For example, unit, tens, hundred, and thousand. After that, calculates its place value and convert into roman numerals.

IntegerToRoman.java

Output: ### Print Roman Numeral in a Given Range

DecimaltoRoman.java

Output: Besides the above methods, there are various ways to convert integer to roman such as Using TreeMap, using switch case, etc.

### Feedback   