Converting Integers to Roman Numerals equivalent in Java
In this post we will see how to convert Integer to Roman numeral in Java. We will also look into Java program to convert integer to roman numerals.
Let's understand what is the Input and the expected output.
Input : 99
Output: XCIX
Input : 81
Output: LXXXI
Input : 0
Output: not defined
Algorithm
STEP 1:
Note down all Unique characters where Roman numbers deviated from usual Pattern and put them in map.
Take an example,
Roman equivalent of '1' is 'I'. So we will add this in map,
map.put(1, "I"); Now no need to add Roman equivalent of '2' as it can be formed from equivalent of '1'
(taking 'I' twice),
Same for 3 (taking 'I' thrice).
This is not the case with Roman equivalent of '4', it has different pattern and not the ('IIII'),
so add it in map. map.put(4, "IV");
Note:
Unique patterns noted here are to support integers from 1 to 399 as program only support till 399.
Map map = new LinkedHashMap();
map.put(100, "C");
map.put(90, "XC");
map.put(50, "L");
map.put(40, "XL");
map.put(10, "X");
map.put(9, "IX");
map.put(5, "V");
map.put(4, "IV");
map.put(1, "I");
If we want program to support more integers then identify patterns where Roman numbers have unusual patterns after 399 and add it in map.
STEP 2:
For converting Integer to Roman equivalent, we will start comparing given Integer with largest number in map,
Eg:
Map map = new LinkedHashMap();
map.put(100, "C");
map.put(90, "XC");
map.put(50, "L");
map.put(40, "XL");
map.put(10, "X");
map.put(9, "IX");
map.put(5, "V");
map.put(4, "IV");
map.put(1, "I");
check how many times given number(say 153) has 100 in it(1 time, So pick "C" and remove 100 from 153, remaining number is 53),
100: Divide 153/100 = 1 time in 153 (remaining number is 153 - 100 = 53), remaining number is 153%100 = 53
then check how many time remaining number(53) has 90 in it(0 time),
90: 53/90 = 0 time in 53 (remaining number 53), remaining number is 53%90 = 53
then check how many time remaining number(53) has 50 in it(1 time, So pick "L" and remove 50 from 53, remaining number is 3),
50: 53/50 = 1 time in 53 (remaining number is 53 - 50 = 3), remaining number is 53 % 50 = 3
then check how many time remaining number(3) has 40 in it(0 time),
40: 3/40 = 0 time in 40 (remaining number 3), remaining number is 3%40 = 3
then check how many time remaining number(3) has 10 in it(0 time),
10: 3/10 = 0 time in 10 (remaining number 3), remaining number is 3%10 = 3
then check how many time remaining number(3) has 9 in it(0 time),
9: 3/9 = 0 time in 9 (remaining number 3), remaining number is 3%9 = 3
then check how many time remaining number(3) has 5 in it(0 time),
5: 3/5 = 0 time in 5 (remaining number 3), remaining number is 3%5 = 3
then check how many time remaining number(3) has 4 in it(0 time),
4: 3/4 = 0 time in 4 (remaining number 3), remaining number is 3%4 = 3
then check how many time remaining number has(3) 1 in it(3 time, So pick "I"*3 = "III" and remove 1 thrice from 3, remaining number is 0),
1: 3/1 = 3 time in 1 (remaining number is 3 - (1+1+1) = 0), remaining number is 3%1 = 0
We reach 0, no more number present, Stop here are return "CLIII"
Convert Integer to Roman numerals Java Program
Another approach
In this post we will see how to convert Integer to Roman numeral in Java. We will also look into Java program to convert integer to roman numerals.
Let's understand what is the Input and the expected output.
Input : 99
Output: XCIX
Input : 81
Output: LXXXI
Input : 0
Output: not defined
Integer to Roman numbers chart |
Algorithm
STEP 1:
Note down all Unique characters where Roman numbers deviated from usual Pattern and put them in map.
Take an example,
Roman equivalent of '1' is 'I'. So we will add this in map,
map.put(1, "I"); Now no need to add Roman equivalent of '2' as it can be formed from equivalent of '1'
(taking 'I' twice),
Same for 3 (taking 'I' thrice).
This is not the case with Roman equivalent of '4', it has different pattern and not the ('IIII'),
so add it in map. map.put(4, "IV");
Note:
Unique patterns noted here are to support integers from 1 to 399 as program only support till 399.
Map
map.put(100, "C");
map.put(90, "XC");
map.put(50, "L");
map.put(40, "XL");
map.put(10, "X");
map.put(9, "IX");
map.put(5, "V");
map.put(4, "IV");
map.put(1, "I");
If we want program to support more integers then identify patterns where Roman numbers have unusual patterns after 399 and add it in map.
STEP 2:
For converting Integer to Roman equivalent, we will start comparing given Integer with largest number in map,
Eg:
Map
map.put(100, "C");
map.put(90, "XC");
map.put(50, "L");
map.put(40, "XL");
map.put(10, "X");
map.put(9, "IX");
map.put(5, "V");
map.put(4, "IV");
map.put(1, "I");
check how many times given number(say 153) has 100 in it(1 time, So pick "C" and remove 100 from 153, remaining number is 53),
100: Divide 153/100 = 1 time in 153 (remaining number is 153 - 100 = 53), remaining number is 153%100 = 53
then check how many time remaining number(53) has 90 in it(0 time),
90: 53/90 = 0 time in 53 (remaining number 53), remaining number is 53%90 = 53
then check how many time remaining number(53) has 50 in it(1 time, So pick "L" and remove 50 from 53, remaining number is 3),
50: 53/50 = 1 time in 53 (remaining number is 53 - 50 = 3), remaining number is 53 % 50 = 3
then check how many time remaining number(3) has 40 in it(0 time),
40: 3/40 = 0 time in 40 (remaining number 3), remaining number is 3%40 = 3
then check how many time remaining number(3) has 10 in it(0 time),
10: 3/10 = 0 time in 10 (remaining number 3), remaining number is 3%10 = 3
then check how many time remaining number(3) has 9 in it(0 time),
9: 3/9 = 0 time in 9 (remaining number 3), remaining number is 3%9 = 3
then check how many time remaining number(3) has 5 in it(0 time),
5: 3/5 = 0 time in 5 (remaining number 3), remaining number is 3%5 = 3
then check how many time remaining number(3) has 4 in it(0 time),
4: 3/4 = 0 time in 4 (remaining number 3), remaining number is 3%4 = 3
then check how many time remaining number has(3) 1 in it(3 time, So pick "I"*3 = "III" and remove 1 thrice from 3, remaining number is 0),
1: 3/1 = 3 time in 1 (remaining number is 3 - (1+1+1) = 0), remaining number is 3%1 = 0
We reach 0, no more number present, Stop here are return "CLIII"
Convert Integer to Roman numerals Java Program
package com.javabypatel; import java.util.LinkedHashMap; import java.util.Map; public class IntegerToRomanNumber { public static void main(String[] args) { System.out.println(getRomanEquivalentOfInteger(399)); } private static String getRomanEquivalentOfInteger(int number){ if(number<=0){ return "not defined"; } //Noting down all Unique characters where Roman numbers deviated from usual Pattern. //unique patterns noted here are to support integers from 1 to 399 as program only support till 399. //if we want program to support more integers then identify patterns where Roman numbers have unusual patterns after 399 and add it in map. Map<Integer, String> map = new LinkedHashMap<Integer, String>(); map.put(100, "C"); map.put(90, "XC"); map.put(50, "L"); map.put(40, "XL"); map.put(10, "X"); map.put(9, "IX"); map.put(5, "V"); map.put(4, "IV"); map.put(1, "I"); String romanEqui=""; // Iterate map, check how many times given number has 100 in it, then check how many time remaining number has 90 in it and so on. // or we can also say, is number divisible by 100, remaining number is divisible by 90 and so on. // if number is 153, then first will see how many time number has 100 in it, which is 1 time. // 100 - 1 time in 150 (remaining number is 150 - 100 = 53) OR 153/100 = 1 remaining 153%100 = 53 // 90 - 0 time in 53 (remaining number is 53 - 90 = 0) OR 53/90 = 0 remaining 53 % 90 = 53 (we only need to find perfectly divisible numbers.) // 50 - 1 time in 53 (remaining number is 53 - 50 = 3) OR 53/50 = 1 remaining 53 % 50 = 3 // 40 - 0 time in 3 (remaining number is 3 - 40 = 0) OR 3/40 = 0 remaining 3 % 40 = 3 // 10 - 0 time in 3 (remaining number is 3 - 10 = 0) OR 3/10 = 0 remaining 3 % 10 = 3 // 9 - 0 time in 3 (remaining number is 3 - 9 = 0) OR 3/9 = 0 remaining 3 % 9 = 3 // 5 - 0 time in 3 (remaining number is 3 - 5 = 0) OR 3/5 = 0 remaining 3 % 5 = 3 // 4 - 0 time in 3 (remaining number is 3 - 4 = 0) OR 3/4 = 0 remaining 3 % 4 = 3 // 1 - 3 time in 3 (remaining number is 3 - 1 = 0) OR 3/1 = 3 remaining 3 % 1 = 0 for (Map.Entry<Integer, String> entry : map.entrySet()) { int key = entry.getKey(); if(number/key!=0){ for (int i = 0; i < (number/key); i++) { romanEqui = romanEqui + map.get(key); } number = number % key; } } return romanEqui; } }
Another approach
package com.javabypatel; public class RomanEquivalent { public static void main(String[] args) { System.out.println(romanEquivalent(4)); } //Another Solution - This method works for first 50 numbers, can be extended by extending uniqueNumber and arrSymbol static String romanEquivalent(int number) { int[] uniqueNumber = new int[] {1, 4, 5, 9, 10, 40, 50}; String[] arrSymbol = new String[]{"I", "IV", "V", "IX", "X", "XL", "L"}; int size = uniqueNumber.length - 1; StringBuffer result = new StringBuffer(); while (number > 0) { int count = number / uniqueNumber[size]; while (count > 0) { result.append(arrSymbol[size]); count--; } number = number % uniqueNumber[size]; size--; } return result.toString(); } }
You may also like to see
Factorial of Big Number using Recursion in Java
Get Level/Height of node in binary tree
Implement Queue using One Stack in Java (Recursive Implementation)
Rotate matrix by 90 degree
Top 25 Multithreading Interview Questions in Java
Find the element that appears once others appears THRICE.
Merge two sorted arrays in Java
Union and Intersection of Two Sorted Arrays
Find all pairs of elements from array whose sum equals to given number K.
Find Minimum length Unsorted Subarray, Sorting which makes the complete array sorted.
Find Largest and Second Largest number in array
How Binary search Algorithm works in Java
Enjoy !!!!
If you find any issue in post or face any error while implementing, Please comment.
Post a Comment