In this tutorial, I am going to discuss how to convert integer to roman numeral.
Given an integer, Write a code to convert it to a Roman numeral.
What is roman numeral?
Roman numerals are represented by seven different letters (I, V, X, L, C, D, M). These seven letters are used to make thousand of numbers.
Example 1 –
Input : 2, Output : II
Example 2 –
Input : 11, Output : XI
Example 3 –
Input : 6, Output : VI
Example 4 –
Input : 57, Output: LVII
NOTE – Range of input should be from 1 to 3999.
Programming Questions on Arrays
Programming Questions on Linked List
Programming Questions on String
Convert Integer to Roman Numeral – Java Code
Before solving this problem, let’s talk about some important points.
The Roman numerals are usually written largest to smallest from left to right. For example – XII (12), VII (7), LVII (57) etc.
However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where we need to do subtraction.
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(1000) to make 400 and 900.
How we are going to solve this problem?
To solve this problem, we can create a map of Roman numerals and it’s corresponding integer value. We also include it’s subtractive cases in a map.
We keep record in a map from largest to smallest, as we want to pick the largest Roman numeral first than smallest one.
Traverse a map and check if input number is greater than the map value. In that case subtract map value from the number and append the corresponding roman numeral in a string builder.
You can check the video tutorial.
Convert roman number to integer value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
//Convert Integer to Roman Numeral public class IntegerToRoman { public static String convertIntegerToRoman(int num) { /* Create a map of Roman numerals and it's corresponding integer value. Also include it's subtractive cases. LinkedHashMap is used to maintain insertion order. */ Map<String, Integer> mp = new LinkedHashMap<>(); mp.put("M", 1000); mp.put("CM",900); mp.put("D", 500); mp.put("CD", 400); mp.put("C", 100); mp.put("XC", 90); mp.put("L", 50); mp.put("XL", 40); mp.put("X", 10); mp.put("IX", 9); mp.put("V", 5); mp.put("IV", 4); mp.put("I", 1); StringBuilder sb = new StringBuilder(); //Traverse a map for(Map.Entry<String, Integer> entry : mp.entrySet()) { while(num >= entry.getValue()) { num = num - entry.getValue(); sb.append(entry.getKey()); } } return sb.toString(); } public static void main(String[] args) { //int num = 4; //int num = 15; int num = 14; String result = convertIntegerToRoman(num); System.out.println(result); } } |
Convert Integer to Roman LeetCode Solution – Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
//Leetcode solution class Solution { public String intToRoman(int num) { Map<String, Integer> mp = new LinkedHashMap<>(); mp.put("M", 1000); mp.put("CM",900); mp.put("D", 500); mp.put("CD", 400); mp.put("C", 100); mp.put("XC", 90); mp.put("L", 50); mp.put("XL", 40); mp.put("X", 10); mp.put("IX", 9); mp.put("V", 5); mp.put("IV", 4); mp.put("I", 1); StringBuilder sb = new StringBuilder(); for(Map.Entry<String, Integer> entry : mp.entrySet()) { while(num >= entry.getValue()) { num = num - entry.getValue(); sb.append(entry.getKey()); } } return sb.toString(); } } |