## Convert Integer to Roman Numerals in JavaIt is a problem frequently asked in interviews of top IT companies like ## Roman NumeralsRoman numerals are the symbolic representation of numbers. These are usually used in watch faces, music theory, etc. There are
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.
## ApproachAn 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.
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 = ## Java Program to Convert Integer to Roman Numerals
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.
## Note: The above Java program works fine up to 3999.## Print Roman Numeral in a Given Range
Besides the above methods, there are various ways to convert integer to roman such as Using TreeMap, using switch case, etc. |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]