The Problem
Write a program to convert Arabic numbers less than 4000 to Roman numeral notation and display the result.
Background & Techniques
A good chance to review your Roman numeral knowledge. A quick Web search will turn up lots of good links to refresh you.
After playing with a few numbers I noticed that the digits 1 through 9 have the same pattern whether they are in the unit's, ten's, hundred's or thousand's position ( thousands up to MMM). The actual characters are different but the pattern is the same.
Units Tens Hundreds Thousands I X C M II XX CC MM III XXX CCC MMM IV XL CD MMMM V L D MMMMM VI LX DC MMMMMM VII LXX DCC etc. VIII LXXX DCCC IX XC CM
If we make a table containing the seven characters I, V, X, L, C, D, M, notice that the units digit uses only the first 3 characters (I. V, X), the tens digit uses 3rd through 5th characters (X, L, C), and the hundreds digit uses the 5th through 7th (C, D, M). The program uses this consistent pattern. Check the code to see how.
Addendum October 8,2005: Finally got around to adding number conversions from Roman back to Arabic at a user's request. A bit more complicated because there are many ways to form invalid Roman numerals using valid letters. In fact a random collection of digits 1 through 9 will always form a valid Arabic number, but a random collection of letters I, V, X, L, C, D, M is quite unlikely to form a valid Roman number.
The lack of a zero placeholder is another problem. Our conversion code needs to identify that no numeral is defined for a particular position so we can insert the missing zero.
|
Try allowing Arabic numbers up to 9999. Notice that above 3999, the leading 'M's just keep increasing. (There are some other conventions using lines over the M's or some such, but they didn't seem to adapt very well to machine display.) |
|
Implemented
October, 2005: |
| Created: September 8, 2000 |
Modified: November 07, 2008 |