Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 33

Convert a Roman numeral to decimal

CStringsJNTUH R22 · CS108ES

Aim

To write a C program that reads a Roman numeral from I to L (1 to 50) and prints its decimal value. It handles the subtractive forms IV, IX and XL, and rejects any character other than I, V, X and L.

Algorithm

  1. Read the Roman numeral as a string.
  2. Set total to 0.
  3. Go through the string from left to right. Find the value of the current letter (I = 1, V = 5, X = 10, L = 50). If the letter is not one of these, print an error and stop.
  4. Find the value of the next letter (0 if there is no next letter).
  5. If the current value is smaller than the next value (as in IV or XL), subtract it from total. Otherwise add it to total.
  6. After the loop, if total is more than 50, print that the numeral is out of range and stop.
  7. Print total.

Program

C
#include <stdio.h>
#include <string.h>

/* Returns the value of one Roman letter, or 0 if the letter is not allowed */
int roman_value(char ch) {
    switch (ch) {
        case 'I': return 1;
        case 'V': return 5;
        case 'X': return 10;
        case 'L': return 50;
        default:  return 0;
    }
}

int main(void) {
    char roman[20];
    printf("Enter a Roman numeral (I to L): ");
    if (scanf("%19s", roman) != 1) {
        printf("Invalid input\n");
        return 1;
    }

    int len = strlen(roman);
    int total = 0;
    for (int i = 0; i < len; i++) {
        int current = roman_value(roman[i]);
        if (current == 0) {
            printf("'%c' is not allowed. Use only I, V, X and L.\n", roman[i]);
            return 1;
        }
        int next = 0;
        if (i + 1 < len) {
            next = roman_value(roman[i + 1]);
        }
        /* A smaller value before a bigger one is subtracted: IV = 5 - 1 */
        if (current < next) {
            total = total - current;
        } else {
            total = total + current;
        }
    }

    if (total > 50) {
        printf("%s is %d, which is outside the range I to L\n", roman, total);
        return 1;
    }
    printf("Decimal value of %s = %d\n", roman, total);
    return 0;
}

Output

Numeral with two subtractive pairs (XL and IV)

Output 1
Enter a Roman numeral (I to L): XLIV
Decimal value of XLIV = 44

Numeral with IX

Output 2
Enter a Roman numeral (I to L): XXIX
Decimal value of XXIX = 29

Invalid character

Output 3
Enter a Roman numeral (I to L): XIZ
'Z' is not allowed. Use only I, V, X and L.

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

The rule used is simple: if a letter's value is smaller than the letter after it, subtract it; otherwise add it. So in XLIV, X (10) comes before L (50) and is subtracted, and I (1) comes before V (5) and is subtracted: −10 + 50 − 1 + 5 = 44.

Only capital letters are accepted. The program checks the characters and the range, but it does not check every rule of Roman numerals. For example, IIII gives 4 and VX gives 5, though neither is written that way. A stricter version could convert the answer back to Roman and compare it with the input.

Common mistakes

  • Adding every letter without the subtraction rule. Then IV gives 6 instead of 4. Compare each letter with the one after it.

  • Reading roman[i + 1] on the last letter without checking i + 1 < len. That reads the '\0' at the end, which happens to work here, but going further past the end reads memory that is not part of the string.

  • Not handling letters other than I, V, X and L. Without the check, an invalid letter counts as 0 and the program prints a wrong answer instead of an error.

  • Using %s in scanf with no width. A long input can overflow the array; write %19s for a 20-character array.

Viva questions

Try to answer each one aloud before you open it.

  • What are the values of the Roman letters I, V, X, L and C?

    I = 1, V = 5, X = 10, L = 50 and C = 100.

  • When is a Roman letter subtracted instead of added?

    When it is written just before a letter of larger value. For example, in IX the I comes before X, so it means 10 − 1 = 9.

  • Why does roman_value return 0 for other characters?

    No Roman letter has the value 0, so 0 can safely mean "not allowed". The main program checks for 0 and prints an error.

  • What is the decimal value of XLIX?

    XL is 40 and IX is 9, so XLIX is 49.

  • Why is the switch statement a good fit for roman_value?

    The function compares one character with several fixed values. A switch with one case per letter is shorter and easier to read than a chain of if-else statements.

Learn the concepts

All Programming for Problem Solving Lab experiments →