Convert Integer to Roman Numeral

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.

Roman Numerals Symbol and It's decimal value.

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.

Convert Integer to Roman Numeral

Programming video tutorials

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 before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (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

Convert Integer to Roman LeetCode Solution – Java

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.