๐Ÿ’ป
B.TECH I YEAR โ€ข I SEMESTER โ€ข R22

Programming for Problem Solving

C Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ I Year ๐Ÿ“š I Semester ๐Ÿ’ป C Programming ๐Ÿงช Lab Program
Selected Program

Write a C program to convert a Roman numeral ranging from I to L to its decimal equivalent.

โ† All Lab Programs

Write a C program to convert a Roman numeral ranging from I to L to its decimal equivalent.

C Program
//to display the contents of a file to standard output device.
#include <stdio.h>

int main() {
    char roman[10];
    int decimal = 0;
    int romanToDecimal(char );
    
    printf("Enter a Roman numeral (I to L): ");
    scanf("%s", roman);
    
    // Convert the Roman numeral to decimal
    for (int i = 0; roman[i] != '\0'; i++) {
        // If the current character represents a smaller value than the next character, subtract it
        if (roman[i] == 'I' && (roman[i+1] == 'V' || roman[i+1] == 'X')) {
            decimal -= romanToDecimal(roman[i]);
        }
        else if (roman[i] == 'X' && (roman[i+1] == 'L')) {
            decimal -= romanToDecimal(roman[i]);
        }
        else {
            decimal += romanToDecimal(roman[i]);
        }
    }

    printf("The decimal equivalent is: %d\n", decimal);
    
    return 0;
}

int romanToDecimal(char roman) {
    if (roman == 'I') return 1;
    else if (roman == 'V') return 5;
    else if (roman == 'X') return 10;
    else if (roman == 'L') return 50;
    return 0;  // In case of invalid character
}

OUTPUT
Enter a Roman numeral (I to L): XLIX
The decimal equivalent is: 49

๐Ÿ’ป Online C Compiler

Run and test this C program online. You can also edit the code before executing it.

๐Ÿ’ก Edit the program in the compiler and click Copy Program to copy the latest edited code.
โ† Back to PPS Lab Programs ๐Ÿ“š All Lab Manuals