Converting Decimal Number lying between 1 to 3999 to Roman Numerals
Last Updated :
30 Apr, 2025
Given an integer, convert it into its equivalent Roman numeral representation.
Note: Following is the list of Roman symbols (including subtractive cases):
Symbol | Value |
---|
I | 1 |
IV | 4 |
V | 5 |
IX | 9 |
X | 10 |
XL | 40 |
L | 50 |
XC | 90 |
C | 100 |
CD | 400 |
D | 500 |
CM | 900 |
M | 1000 |
Examples:
Input: 9
Output: IX
Explanation: 9 is written as "IX" in Roman numerals using subtractive notation — placing a smaller numeral before a larger one.
I
= 1, X
= 10IX
means 10 - 1 = 9
Input: 40
Output: XL
Explanation: 40 is written as "XL" in Roman numerals using subtractive notation — placing a smaller numeral before a larger one.
X
= 10, L = 50XL
means 50 - 10 = 40
[General Purpose Solution] - O(n) Time and O(n) Space
Compare given number with base values in the order 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1. Once we find the largest base value that is less than the given number, we divide the number with the base value and repeat the process for smaller base values and quotient. We add the roman symbol corresponding to the base value found to the result, number of times equal to the quotient and repeat the process for remainder.
Let us understand the approach with an example 3549
Iteration 1
- Since 3549 >= 1000 ; largest base value will be 1000 initially.
- Divide 3549/1000. Quotient = 3, res = "MMM" (Note M belongs to 1000)
- Remainder = 549
Iteration 2
- 1000 > 549 >= 500 ; largest base value will be 500.
- Divide 549/500. Quotient = 1, .res = "MMMD"
- Remainder = 49
Iteration 3
- 50 > 49 >= 40 ; largest base value is 40.
- Divide 49/40. Quotient = 1, res = "MMMDXL"
- Remainder = 9.
Iteration 4
- Number 9 is present in list. res = "MMMDXL"
- Remainder = 0.
C++
#include <bits/stdc++.h>
using namespace std;
// Function to convert decimal to Roman Numerals
string toRoman(int x) {
// array of values and symbols
vector<int> base = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
vector<string> sym = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
// to store result
string res = "";
// Loop from the right side to find
// the largest smaller base value
int i = base.size() - 1;
while (x > 0) {
int div = x / base[i];
while (div) {
res += sym[i];
div--;
}
// Repeat the process for remainder
x = x % base[i];
i--;
}
return res;
}
int main() {
int x = 3549;
cout << toRoman(x);
return 0;
}
Java
// Function to convert decimal to Roman Numerals
public class RomanConverter {
public static String toRoman(int x) {
// array of values and symbols
int[] base = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
String[] sym = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
// to store result
StringBuilder res = new StringBuilder();
// Loop from the right side to find
// the largest smaller base value
int i = base.length - 1;
while (x > 0) {
int div = x / base[i];
while (div > 0) {
res.append(sym[i]);
div--;
}
// Repeat the process for remainder
x = x % base[i];
i--;
}
return res.toString();
}
public static void main(String[] args) {
int x = 3549;
System.out.println(toRoman(x));
}
}
Python
# Function to convert decimal to Roman Numerals
def to_roman(x):
# array of values and symbols
base = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]
sym = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"]
# to store result
res = ""
# Loop from the right side to find
# the largest smaller base value
i = len(base) - 1
while x > 0:
div = x // base[i]
while div:
res += sym[i]
div -= 1
# Repeat the process for remainder
x %= base[i]
i -= 1
return res
x = 3549
print(to_roman(x))
C#
// Function to convert decimal to Roman Numerals
public class RomanConverter {
public static string ToRoman(int x) {
// array of values and symbols
int[] baseValues = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
string[] symbols = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
// to store result
string res = "";
// Loop from the right side to find
// the largest smaller base value
int i = baseValues.Length - 1;
while (x > 0) {
int div = x / baseValues[i];
while (div > 0) {
res += symbols[i];
div--;
}
// Repeat the process for remainder
x %= baseValues[i];
i--;
}
return res;
}
public static void Main() {
int x = 3549;
Console.WriteLine(ToRoman(x));
}
}
JavaScript
// Function to convert decimal to Roman Numerals
function toRoman(x) {
// array of values and symbols
const base = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
const sym = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"];
// to store result
let res = "";
// Loop from the right side to find
// the largest smaller base value
let i = base.length - 1;
while (x > 0) {
let div = Math.floor(x / base[i]);
while (div) {
res += sym[i];
div--;
}
// Repeat the process for remainder
x %= base[i];
i--;
}
return res;
}
let x = 3549;
console.log(toRoman(x));
Time Complexity: O(n), where n is the length of the answer string that stores the conversion.
Auxiliary Space: O(n)
[For Limited Range] - O(n) Time and O(n) Space
The idea is based on the fact that we have a limited range to covert 0 to 3999. We isolate the digits corresponding to the thousands, hundreds, tens, and ones places, and then mapping each digit to its respective Roman numeral equivalent based on its positional value.
- Store mappings of character M for different quotients, 0, 1, 2, 3
- Store mappings of C, L and I for different quotients form 0 to 9.
Using the above mappings, we directly generate the result string.
C++
#include <bits/stdc++.h>
using namespace std;
// Function to convert decimal to Roman Numerals
string toRoman(int val) {
// storing roman values of digits from 0-9
// when placed at different places
vector<string> m = {"", "M", "MM", "MMM"};
vector<string> c = {"", "C", "CC", "CCC", "CD",
"D", "DC", "DCC", "DCCC", "CM"};
vector<string> x = {"", "X", "XX", "XXX", "XL",
"L", "LX", "LXX", "LXXX", "XC"};
vector<string> i = {"", "I", "II", "III", "IV",
"V", "VI", "VII", "VIII", "IX"};
// Converting to roman
string thousands = m[val / 1000];
string hundreds = c[(val % 1000) / 100];
string tens = x[(val % 100) / 10];
string ones = i[val % 10];
string ans = thousands + hundreds + tens + ones;
return ans;
}
int main() {
int val = 3549;
cout << toRoman(val);
return 0;
}
Java
import java.util.*;
public class GfG {
// Function to convert decimal to Roman Numerals
public static String toRoman(int val) {
// storing roman values of digits from 0-9
// when placed at different places
String[] m = {"", "M", "MM", "MMM"};
String[] c = {"", "C", "CC", "CCC", "CD",
"D", "DC", "DCC", "DCCC", "CM"};
String[] x = {"", "X", "XX", "XXX", "XL",
"L", "LX", "LXX", "LXXX", "XC"};
String[] i = {"", "I", "II", "III", "IV",
"V", "VI", "VII", "VIII", "IX"};
// Converting to roman
String thousands = m[val / 1000];
String hundreds = c[(val % 1000) / 100];
String tens = x[(val % 100) / 10];
String ones = i[val % 10];
String ans = thousands + hundreds + tens + ones;
return ans;
}
public static void main(String[] args) {
int val = 3549;
System.out.println(toRoman(val));
}
}
Python
# Function to convert decimal to Roman Numerals
def toRoman(val):
# storing roman values of digits from 0-9
# when placed at different places
m = ["", "M", "MM", "MMM"]
c = ["", "C", "CC", "CCC", "CD",
"D", "DC", "DCC", "DCCC", "CM"]
x = ["", "X", "XX", "XXX", "XL",
"L", "LX", "LXX", "LXXX", "XC"]
i = ["", "I", "II", "III", "IV",
"V", "VI", "VII", "VIII", "IX"]
# Converting to roman
thousands = m[val // 1000]
hundreds = c[(val % 1000) // 100]
tens = x[(val % 100) // 10]
ones = i[val % 10]
ans = thousands + hundreds + tens + ones
return ans
if __name__ == "__main__":
val = 3549
print(toRoman(val))
C#
using System;
public class GfG {
// Function to convert decimal to Roman Numerals
public static string toRoman(int val) {
// storing roman values of digits from 0-9
// when placed at different places
string[] m = {"", "M", "MM", "MMM"};
string[] c = {"", "C", "CC", "CCC", "CD",
"D", "DC", "DCC", "DCCC", "CM"};
string[] x = {"", "X", "XX", "XXX", "XL",
"L", "LX", "LXX", "LXXX", "XC"};
string[] i = {"", "I", "II", "III", "IV",
"V", "VI", "VII", "VIII", "IX"};
// Converting to roman
string thousands = m[val / 1000];
string hundreds = c[(val % 1000) / 100];
string tens = x[(val % 100) / 10];
string ones = i[val % 10];
string ans = thousands + hundreds + tens + ones;
return ans;
}
public static void Main(string[] args) {
int val = 3549;
Console.WriteLine(toRoman(val));
}
}
JavaScript
// Function to convert decimal to Roman Numerals
function toRoman(val) {
// storing roman values of digits from 0-9
// when placed at different places
let m = ["", "M", "MM", "MMM"];
let c = ["", "C", "CC", "CCC", "CD",
"D", "DC", "DCC", "DCCC", "CM"];
let x = ["", "X", "XX", "XXX", "XL",
"L", "LX", "LXX", "LXXX", "XC"];
let i = ["", "I", "II", "III", "IV",
"V", "VI", "VII", "VIII", "IX"];
// Converting to roman
let thousands = m[Math.floor(val / 1000)];
let hundreds = c[Math.floor((val % 1000) / 100)];
let tens = x[Math.floor((val % 100) / 10)];
let ones = i[val % 10];
let ans = thousands + hundreds + tens + ones;
return ans;
}
let val = 3549;
console.log(toRoman(val));
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read