Friday, 17 December 2010

Algorithm for To convert roman number to it’s decimal equivalent

Description:


In this program we have to take the roman value. This value is converted into a it’s equivalent decimal number.
Ex:  X=10


Algorithm:


Step 1: Start
Step 2: read the roman numerical as string
Step 3: find length of roman numerical
Step 4: for each charcter in the string

i.        if(char=I) then decimal=1
ii.        if(char=V) then decimal=5
iii.        if(char=X) then decimal=10
iv.        if(char=L) then decimal=50
v.        if(char=C) then decimal=100
vi.        if(char=D) then decimal=500
vii.        if(char=M) then decimal=1000
viii.        otherwise invalid character
Step 5: repeat step 4 until the length of the string
Step 6: k=char[length-1]
Step 7: for each character of decimal string
i)         if(decimal[i]>dec[i-1]) then k=k-decimal[i-1]
ii)       else
       if(decimal[i]=decimal[i-1} or decimal[i]<decimal[i-1])                       then k=k+decimall[i-1]
Step 8: repeat step 7 until the length of decimal string
Step 9: print decimal value

No comments:

Post a Comment