Professional Documents
Culture Documents
Input : 9
Output : IX
Input : 40
Output : XL
Input : 1904
Output : MCMIV
Following is the list of Roman symbols which include subtractive cases also:
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
Idea is to convert the units, tens, hundreds, and thousands places of the given
number separately. If the digit is 0, then there’s no corresponding Roman numeral
symbol. The conversion of digit’s 4’s and 9’s are little bit different from other
digits because these digits follows subtractive notation.
Output:
MMMDXLIX
Explanation:
Step 1
Initially number = 3549
Since 3549 >= 1000 ; largest base value will be 1000 initially.
Divide 3549/1000. Quotient = 3, Remainder =549. The corresponding symbol M will
be repeated thrice.
Step 2
Now, number = 549
1000 > 549 >= 500 ; largest base value will be 500.
Divide 549/500. Quotient = 1, Remainder =49. The corresponding symbol D will be
repeated once.
Step 3
Now, number = 49
50 > 49 >= 40 ; largest base value is 40.
Divide 49/40. Quotient = 1, Remainder = 9. The corresponding symbol XL will be
repeated once.
Step 4
Now, number = 9
10> 9 >= 9 ; largest base value is 9.
Divide 9/9. Quotient = 1, Remainder = 0. The corresponding symbol IX will be
repeated once.
Step 5
filter_none
edit
play_arrow
brightness_4
// C++ Program to convert decimal number to
// roman numerals
#include <bits/stdc++.h>
using namespace std;
//Driver program
int main()
{
int number = 3549;
printRoman(number);
return 0;
}
Output:
References : http://blog.functionalfun.net/2009/01/project-euler-89-converting-to-
and-from.html
Divide the given number into digits at different places like one’s , two’s ,
hundred’s or thousand’s.
Starting from the thousand’s place print the corresponding roman value. For
example, if the digit at thousand’s place is 3 then print the roman equivalent of
3000.
Repeat the second step until we reach one’s place.
Example:
Suppose the input number is 3549. So, starting from thousand’s place we will start
printing the roman equivalent. In this case we will print in the order as given
below:
First: Roman equivalent of 3000
Second: Roman equivalent of 500
Third: Roman equivalent of 40
Fourth: Roman equivalent of 9
So, the output will be: MMMDXLIX
filter_none
edit
play_arrow
brightness_4
// C++ Program for above approach
#include <bits/stdc++.h>
using namespace std;
// Converting to roman
string thousands = m[num/1000];
string hundereds = c[(num%1000)/100];
string tens = x[(num%100)/10];
string ones = i[num%10];
return ans;
}
Output:
MMMDXLIX
Also, lets maintain a dictionary called romanNumeral = {1 : ‘I’, 5: ‘V’, 10: ‘X’,
50: ‘L’, 100: ‘C’, 500: ‘D’, 1000: ‘M’}
Example:
Suppose the input number is 3649.
Iter 1
Initially number = 3649
main significant digit is 3. Div = 1000.
So, romanNumeral[1000] * 3
gives: MMM
Iter 2
now, number = 649
main significant digit is 6. Div = 100.
So romanNumeral[100*5] + (romanNumeral[div] * ( 6-5))
gives: DC
Iter 3
now, number = 49
main significant digit is 4. Div = 10.
So romanNumeral[10] + romanNumeral[10 * 5]
gives: XL
Iter 4
now, number = 9
main significant digit is 9. Div = 1.
So romanNumeral[1] * romanNumeral[1*10]
gives: IX
`:wq