PPS Lab · Experiment 34
Convert a decimal number to Roman numeral
CStringsJNTUH R22 · CS108ES
Aim
To write a C program that reads a number from 1 to 50 and prints its Roman numeral equivalent.
Algorithm
- Store the values 50, 40, 10, 9, 5, 4, 1 in one array and their Roman symbols L, XL, X, IX, V, IV, I in another array, in the same order.
- Read the number. If it is not between 1 and 50, print an error and stop.
- Start with the first (largest) value.
- While the number is greater than or equal to the current value, print its symbol and subtract the value from the number.
- Move to the next value and repeat step 4, until all values are used.
- The symbols printed together form the Roman numeral.
Program
#include <stdio.h>
/* Prints the Roman numeral for num, where num is from 1 to 50 */
void print_roman(int num) {
int values[] = {50, 40, 10, 9, 5, 4, 1};
const char *symbols[] = {"L", "XL", "X", "IX", "V", "IV", "I"};
for (int i = 0; i < 7; i++) {
while (num >= values[i]) {
printf("%s", symbols[i]);
num = num - values[i];
}
}
}
int main(void) {
int num;
printf("Enter a number (1 to 50): ");
if (scanf("%d", &num) != 1) {
printf("Invalid input\n");
return 1;
}
if (num < 1 || num > 50) {
printf("The number must be between 1 and 50\n");
return 1;
}
printf("Roman equivalent of %d = ", num);
print_roman(num);
printf("\n");
return 0;
}Output
Number with subtractive forms
Enter a number (1 to 50): 49
Roman equivalent of 49 = XLIXNumber with repeated letters
Enter a number (1 to 50): 38
Roman equivalent of 38 = XXXVIIINumber out of range
Enter a number (1 to 50): 75
The number must be between 1 and 50Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
Putting the two-letter forms 40 (XL), 9 (IX) and 4 (IV) in the table is what makes the simple "take the biggest value that fits" method work. Without them, 4 would come out as IIII and 9 as VIIII.
For 49: 40 fits once (XL, leaves 9), then 9 fits once (IX, leaves 0), so the answer is XLIX.
Common mistakes
Leaving out 40, 9 and 4 from the table. The program then prints
XXXXfor 40 andIIIIfor 4, which are not correct Roman numerals.Keeping the values out of order. The method only works if the values go from largest to smallest.
Using
ifinstead ofwhilefor each value. A value like 10 can be needed more than once (30 = XXX), so it must repeat while the number is still large enough.Not checking the range. The table stops at L (50), so a number like 75 would print
LXXV, which is outside what the program was asked to handle.
Viva questions
Try to answer each one aloud before you open it.
Why are XL, IX and IV kept in the table as separate entries?
They are the subtractive forms. Keeping them in the table lets the program print 40, 9 and 4 correctly with the same "largest value first" loop.
What is the Roman numeral for 38?
30 is XXX and 8 is VIII, so 38 is XXXVIII.
Why is an array of strings (const char *symbols[]) used instead of an array of characters?
Some symbols, such as XL and IX, have two letters. A string can hold more than one letter; a single
charcannot.What kind of method is "take the largest value that fits, then repeat"?
It is called a greedy method: at each step it takes the biggest piece it can.