Convert Integer to Roman Numerals in JavaIt 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 NumeralsRoman 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.
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:
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:
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 NumeralsIntegerToRoaman.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: Note: The above Java program works fine up to 3999.Print Roman Numeral in a Given RangeDecimaltoRoman.java Output: Besides the above methods, there are various ways to convert integer to roman such as Using TreeMap, using switch case, etc. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India