CS1101C Lab 2 (Odd Week)
Roman Numerals
The deadline for this lab question is Thursday 02 March 2006,
23:59:59 hours.
The name of your C program file must be called roman.c,
files with any other name will not be marked.
Preliminary
Roman numerals are used frequently to indicate dates, in particular the year (for example, MMVI represents the year 2006). There are two main differences between roman and arabic numerals.
- There is no symbol for zero in roman numerals;
- Numeral placement in the roman system can indicate either addition or subtraction.
Your task for this lab assignment is to convert a number ranging from 1 to 9999 to its roman numeral equivalent.
Roman Numeral Conversion
The conversion is specified using the following cases.
- I represents 1. Joining two I's together (i.e. II) represents 2, while III represents 3. However, IIII to represent 4 is forbidden because there is a shorter notation.
- V represents 5. Placing I before V (i.e. IV) will mean subtract I from V, i.e. 4. Note that we do not subtract more than one I from V, so IIV to represent 3 is invalid. Placing I after V (i.e. VI) will mean add I to V, i.e. 6. Likewise, VII and VIII represents 7 and 8. To represent 9 and 10, we do not use VIIII and VV respectively because there exist shorter notations.
- X represents 10. Following the rule above, IX is 9 while XI is 11.
- L represents 50 and works in much the same way as V. So XL is 40 while LX is 60. Additionally, XLI is 41, XLV is 45, XLIV is 44, XLVI is 46, XLIX is 49. Hopefully you get the picture...
- C represents 100.
- D represents 500.
- M represents 1000. M is the last character of the roman numeral system so to represent 9000, there is no choice but to use MMMMMMMMM.
The Task
Write a program roman.c that requests a year from the user. Assuming that the executable is roman, a sample run of the program is shown below. User input is denoted in bold.
$ gcc -Wall roman.c -o roman
$ roman
Enter the year : 1
Year 1 in roman numerals is I.
$ roman
Enter the year : 2006
Year 2006 in roman numerals is MMVI.
$ roman
Enter the year : 1998
Year 1998 in roman numerals is MCMXCVIII.
$ roman
Enter the year : 9999
Year 9999 in roman numerals is MMMMMMMMMCMXCIX.
$ roman
Enter the year : 9888
Year 9888 in roman numerals is MMMMMMMMMDCCCLXXXVIII.
$
Points to Note
- For simplicity, we assume that user input will always be within the range of permissible values, so there is no need to perform any explicit checks on the data entered by the user.
This document, index.html, has been accessed 13 times since 25-Jun-24 11:57:13 +08.
This is the 1st time it has been accessed today.
A total of 9 different hosts have accessed this document in the
last 391 days; your host, 216.73.216.149, has accessed it 1 times.
If you're interested, complete statistics for
this document are also available, including breakdowns by top-level
domain, host name, and date.