How to convert decimal numbers to Roman numerals
Roman numerals, a number system originating from ancient Rome, are still widely used today in various contexts, such as clock faces, book chapters, and historical references. Converting decimal numbers to Roman numerals involves translating a base-10 number into its equivalent in this classical numeral system. This conversion process is not only a fascinating exercise in understanding ancient numeral systems but also a valuable skill in applications where Roman numerals are preferred for their aesthetic or traditional value.
Problem statement
Given a decimal number, the task is to convert it to Roman numerals using a
Representation
Symbols in Roman numerals are formed by combining specific base values. For example, “M” indicates 1000, whereas “CM” is a combination of “C” (100) and “M,” resulting in 900. Similarly, “XC” reflects 90, with “X” (10) subtracted from “C.”
Note: To explore further details about Roman numerals, please refer to the Roman to integer problem.
The following table shows the important Roman numeral symbols for the suitable values:
Value | Symbol |
1000 | M |
900 | CM |
500 | D |
400 | CD |
100 | C |
90 | XC |
50 | L |
40 | XL |
10 | X |
9 | IX |
5 | V |
4 | IV |
1 | I |
Algorithm
Here’s the algorithm for converting decimal values to Roman numerals:
Begin with the provided decimal number.
Determine the largest base value that is less than or equal to the current number.
Divide this number by the determined base value.
Repeat the corresponding Roman numeral symbol in the result quotient times.
For subsequent division and repetitions, replace the number with the remainder.
Continue this process until the number reaches zero.
Decimal to Roman numeral conversion process
The tables below demonstrate the step-by-step conversion of decimal numbers to their Roman numeral equivalents. Each row represents a key step in the algorithm, such as identifying the largest base value, computing the quotient and remainder, determining the Roman numeral symbol, and incrementally updating the result list.
Note: The "Number" column represents the value utilized for further division in each iteration, providing a more concise and informative representation of the conversion process.
Example 1: Convert 789 to Roman numerals
Iteration | Number | Largest Base Value | Quotient | Remainder | Roman Numeral | Updated Result List |
1 | 789 | 500 | 1 | 289 | D | D |
2 | 289 | 100 | 2 | 89 | C | DC |
3 | 89 | 50 | 1 | 39 | L | DCL |
4 | 39 | 10 | 3 | 9 | X | DCLXXX |
5 | 9 | 9 | 1 | 0 | IX | DCLXXXIX |
Example 2: Convert 1492 to Roman numerals
Iteration | Number | Largest Base Value | Quotient | Remainder | Roman Numeral | Updated Result List |
1 | 1492 | 1000 | 1 | 492 | M | M |
2 | 492 | 400 | 1 | 92 | CD | MCD |
3 | 92 | 90 | 1 | 2 | XC | MCDXC |
4 | 2 | 1 | 2 | 0 | II | MCDXCII |
Note: These tables give a detailed breakdown of the conversion process, demonstrating how each Roman numeral is obtained from the original decimal value.
Code example
The example is as follows:
#include <iostream>using namespace std;string convertToRoman(int decimal) {string symbols[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};int remaining = decimal;string result = "";for (size_t i = 0; i < sizeof(symbols) / sizeof(symbols[0]); ++i) {while (remaining >= values[i]) {result += symbols[i]; // Append the current symbol to the resultremaining -= values[i]; // Subtract the current value from the remaining}}return result;}int main() {int decimalNumber = 789;// Display the original decimal numbercout << "C++ code" << endl;cout << "Decimal Number: " << decimalNumber << endl;// Convert the decimal number to Roman numeralsstring romanNumeral = convertToRoman(decimalNumber);// Display the Roman numeral representationcout << "Roman Numeral: " << romanNumeral << endl;return 0;}
Code explanation
This code implements a common approach for converting decimal values to Roman numerals across several programming languages:
Function definition: The
convertToRomanfunction accepts an integerdecimalas input and returns a string with the matching Roman numeral.Symbol and value definitions: The function initializes two lists:
symbolswhich contain Roman numeral symbols andvalueswhich have the matching decimal values.Initialization: Variables such as
remaining(initialized with the inputdecimal) andresult(initialized as an empty string) are set up.Iteration through symbols and values: A loop traverses the
symbolsandvalues, identifying the largest base value that is less than or equal to theremainingdecimal.Symbol repetition: Within the loop, the code repeats the current symbol while the
remainingvalue is greater or equal to the current value. The symbol is appended to theresultstring.Value subtraction: The code subtracts the current value from the
remaining, updating it for future iterations.Result string building: The loop continues until the
remainingvalue becomes zero. Theresultstring accumulates the repeatedsymbols.Return result: The function returns the final
resultstring, representing the Roman numeral equivalent of the input decimal.Main function: The
mainfunction demonstrates the usage of the conversion function by providing an example decimal number (e.g.,789).
Complexity
Time complexity: The time complexity of the algorithm is
, where is the input decimal number. The loop iterates through the base values, reducing the decimal number with each iteration. Space complexity: The space complexity is
because the algorithm uses only a constant amount of extra space for variables, making it space-efficient regardless of the input size.
Free Resources