How convert Decimal number to Roman number.

Julian Saenz
2 min readOct 14, 2020

In this case we’re going to write a algorithm that convert a decimal number to roman number, just for fun…

This morning i wake up with that curiosity, now how do that? just open your favorite IDE(Integrated Development Environment), piece of paper, pen and think.

Let’s put some constrains…

The input always will be > 0 and < 4000. Then you will realize why!

First, let’s use a dictionary to identify the keys and compare with the number we want to convert. Think something similar to when we’re going to convert a number to binary.

Now we are going to go through the keys of the dictionary and subtract the value of the key with the number we want to convert.

We must have something very present and it’s that in the Roman numbers we can write the same symbol maximum 3 times, never 4 (the ‘I’ is an exception but we aren’t going to take it here).

Where 0 < decimal < 4000

Now, just print(“roman”) and be happy :)

The reason why it is only possible to convert numbers from 1 to 3999 (at least in this code) is because in the Roman numbering system when it reaches 5000 its representation is equal to “V” but with a small line similar to the “Ñ” and didn’t know how to represent it with the ASCII code.

Finally, the Romans didn’t use the 0 because not exist at that time, it is believed that the first to use the 0 were the Maya civilization.

--

--