Decimal to Ruman Numeral Converter Using python

Decimal to Ruman Numeral Converter Using python

How to convert Decimal or hindu-Arabic to Roman Numeral using Python.

In my last article I discuss how to create roman numeral to Decimal converter using using Python. today we shall be dealing with how to do the opposite. ie how to convert from Decimal to Roman Numeral. firstly let's see the example without codes and study the logic behind it.

DecimalLogical ExplanationRoman Numeral
22051000+1000+595
21071000+900+100+5+1+1MCMCVII
259100+100+50+9
494400+90+4
25681000+1000+500+50+10+5+1+1+1MMDLXVIII

If you study the logical explanation well you will notice that some Remainder which are not usually stated in Roman symbol is there. Remainder like 900,90,40 etc. Now the below table show the All important roman Symbol that you should know.

DecimalRoman Numeral
1I
4IV
5V
9IX
10X
40XL
50L
90XC
100C
400CD
500D
900CM
1000M

How to write codes for Decimal to roman Numeral converter in python

Now, that we have understand the nature of the problem how do we write the codes to do the work?? firstly: be aware that you need to start picking each of the Remainders with symbol above to divide the given Remainder to get the quotient and the remainder will also be noted. this will let's know the Remainder of times we will need to write each symbol. consider the example below.

To convert 2997 to roman numeral this is how the algorithm will look like

Each Roman NumeralQuotient and Remainder
for M--> 2997/1000.Q=2 and R=997
for CM-->997/900.Q=1 and R=97
for D->97/500.Q=0 and R=97
for CD->97/400.Q=0 and R=97
for C->97/100.Q=0 and R=97
for XC->97/90.Q=1 and R=7
for L->7/50.Q=0 and R=7
for XL->7/40.Q=0 and R=7
for X->7/10.Q=0 and R=7
for IX->7/9.Q=0 and R=7
for V->7/5.Q=1 and R=2
for IV->2/4.Q=0 and R=2
for I->2/1.Q=2 and R=97

Notice the pattern the remainder will be used in the next line just like a method called chinese remainder theorem in mathematics. so your answer wiill be each symbol multiply by its Quotient. i.e 2997=MMCMXCVII notice that M is written twice because its quotient is 2 while those one with 0 as quotient is neglected.

Now, it is time to write codes. Codes>>

number=int(input('Enter your number to be converted to Roman Numeral: '))
quotientOfM=number//1000 #get the number of M
M=quotientOfM*'M'#to write the M in specific times
Remainder=number%1000 

quotientOfCM=Remainder//900 #get the number of CM
CM=quotientOfCM*'CM'#to write the CM in specific times
Remainder=Remainder%900

quotientOfD=Remainder//500 #get the number of CD
D=quotientOfD*'D'#to write the D in specific times
Remainder=Remainder%500 #get the remainder when remainder of CM is divided by 500

quotientOfCD=Remainder//400 #get the number of CD
CD=quotientOfCD*'CD' #to write the CD in specific times
Remainder=Remainder%400 #get the remainder when remainder of CM is divided by 400

quotientOfC=Remainder//100#get the number of C
C=quotientOfC*'C'
Remainder=Remainder%100

quotientOfXC=Remainder//90#get the number of XC
XC=quotientOfXC*'XC'#to write the XC in specific times
Remainder=Remainder%90

quotientOfL=Remainder//50 #get the number of L
L=quotientOfL*'L'#to write the L in specific times
Remainder=Remainder%50

quotientOfXL=Remainder//40#get the number of XL
XL=quotientOfXL*'XL' #to write the XL in specific times
Remainder=Remainder%40

quotientOfX=Remainder//10 #get the number of X
X=quotientOfX*'X'
Remainder=Remainder%10

quotientOfCM=Remainder//9#get the number of IX
IX=quotientOfCM*'IX'
Remainder=Remainder%9

quotientOfV=Remainder//5 #get the number of V
V=quotientOfV*'V'
Remainder=Remainder%5

quotientOfIV=Remainder//4 #get the number of IV
IV=quotientOfIV*'IV'
Remainder=Remainder%4

quotientOfI=Remainder//1#get the number of I
I=quotientOfI*'I'
result=M+CM+D+CD+C+XC+L+XL+X+IX+V+IV+I # to marge all the symbol together
print(result)

Now you can run the codes and see input any value to see the result. that is interesting right?? well, only a beginner will solve the problem like that. since we are actually doing repetition of a pattern in our codes above then we can eventually reduce the codes. Hence use loop. see the below codes also to solve the same problem.

number=int(input('Enter your Number to be converted to Roman Numeral'))
result=''
allSymbol=['M','CM','D',"CD",'C','XC','L','XL','X','IX','V','IV','I']
value=[1000,900,500,400,100,90,50,40,10,9,5,4,1]
for index in range(len(value)):
    quotient=number//value[index] # to know the number of times the symbol should be written
    symbol=quotient*allSymbol[index] #to write the symbol in specific times
    result+=symbol #this is to add the symbol to the result.
    number=number%value[index]#this is to get the remainder which will be use again
print(result)

run the the codes and see how it works. did you realize the power of loop?? yeah, that is how to do it in a simple way. I hope you enjoy this?? you can drop your comment below or chat me up if you have any question or correction on 09153036869 it is still your guy Maxwizard! enjoy coding!