PROGRAMMING EXERCISE

Roman Numerals

 

Write a program that will convert an integer in the range of 1 through 3999 to its equivalent in Roman numerals.

 

The rules for Roman numerals are as follows:

·         The basic symbols are I (= 1), V (= 5), X (= 10), L (= 50), C (= 100), D (= 500), and M (= 1000).

·         If a letter is immediately followed by one of equal or lesser value, the two numbers are added; thus, XX = 20, XV = 15, VI = 6.

·         If a letter is immediately followed by one of greater value, the first is subtracted from the second; thus IV = 4, XL = 40, CM = 900.

·         A bar over a letter multiplies it by 1000; thus, an X with a bar over it = 10,000.  Such numbers will not be addressed by this project.

 

Examples:

The numbers from 1 to 10 are: I, II, III, IV, V, VI, VII, VIII, IX, X.

XLVII = 47, CXVI = 116, MCXX = 1120, MCMXIV = 1914.

 

Note that a given symbol appears no more than three times consecutively in a number.  This is why 4 is written as "IV" instead of "IIII", and 40 is written as "XL" instead of "XXXX".

 

Sample runs are shown below:

 

 

 

 

Download the solution for this project here.

 

Enhancement

 

Modify the program so that it converts either way: decimal to Roman (as in the program above) OR Roman to decimal – allow the user to specify what type of conversion they want to do.

 

Probably the most challenging part of converting a Roman Numerals number to decimal is not the conversion itself, but rather validating the input. Basic validation would include converting keystroke input to uppercase and allowing only the characters I, V, X, L, C, D, and M to be entered. Beyond that, the following rules should be applied:

·         D, L, or V may each only appear at most one time in the string

·         M, C, X, or I may appear no more that three times consecutively in the string

·         The following pairs of letters are invalid in all cases: IL, IC, ID, IM, XD, XM, VX, VL, VC, VD, VM, LC, LD, LM, DM.

·         Once a letter has been subtracted from, neither it nor its "5 counterpart" may appear again in the string - so neither X nor V can follow IX, neither C nor L may follow XC, and neither M nor D may follow CM.

·         Once a letter has been used as a subtraction modifier, it cannot appear again in the  string - so C cannot follow CD or CM, X cannot follow XL or XC, and I cannot follow IV or IX.

·         Once I, X, or C (or their "5-counterparts" V, L, and D) appears in a string, the I, X, or C cannot subsequently be used as subtraction modifiers - so IV or IX cannot follow I or V, XL or XC cannot follow X or L, and CD or CM cannot follow C or D.

 

Sample runs are shown below. (Note: The sample program implementation uses option buttons enclosed in a frame. These controls are discussed in later tutorials.)

 

When the user selects "Decimal Number to Roman Numerals" for the conversion type, the program operates as in the original sample program:

 

 

When the user selects "Roman Numerals to Decimal Number" for the conversion type, note that the captions change and the reverse conversion is performed:

 

 

Example of invalid input:

 

 

 

Download the solution for this project here.

 

Further information on Roman Numerals can be found on these websites:

 

http://mathforum.org/dr.math/faq/faq.roman.html

 

http://www.novaroma.org/via_romana/numbers.html