Introduction:
This C program converts Roman numerals into their decimal equivalents. Roman numerals are a numeral system originating from ancient Rome, employing combinations of letters from the Latin alphabet (I, V, X, L, C, D, M) to represent values. The program interprets valid Roman numeral strings and computes their corresponding decimal values, making it a useful tool for various applications that require conversion between these numeral systems.
#include<stdio.h>
#include<string.h>
int digit(char);
int main(){
char roman_Number[1000];
int i=0;
long int number =0;
printf("Enter any roman number : \n");
scanf("%s",roman_Number);
while(roman_Number[i]){
if(digit(roman_Number[i]) < 0){
printf("Invalid roman digit : %c",roman_Number[i]);
return 0;
}
if((strlen(roman_Number) -i) > 2){
if(digit(roman_Number[i]) < digit(roman_Number[i+2])){
printf("Invalid roman number");
return 0;
}
}
if(digit(roman_Number[i]) >= digit(roman_Number[i+1]))
number = number + digit(roman_Number[i]);
else{
number = number + (digit(roman_Number[i+1]) - digit(roman_Number[i]));
i++;
}
i++;
}
printf("Its decimal value is : %ld",number);
return 0;
}
int digit(char c){
int value=0;
switch(c){
case 'I': value = 1; break;
case 'V': value = 5; break;
case 'X': value = 10; break;
case 'L': value = 50; break;
case 'C': value = 100; break;
case 'D': value = 500; break;
case 'M': value = 1000; break;
case '\0': value = 0; break;
default: value = -1;
}
return value;
}
Include Header Files:
#include <stdio.h>
: Necessary for standard input and output functions likeprintf
andfgets
.#include <string.h>
: Included for string manipulation functions likestrlen
.
Function to Convert Roman to Decimal:
int romanToDecimal(char *roman)
: This function takes a string of Roman numerals and returns its decimal equivalent.
Variable Declarations:
int decimal = 0;
: Initializes a variable to store the final decimal value.int prevValue = 0;
: Stores the value of the previous Roman numeral for comparison.
Loop Through the Roman Numeral:
- The
for
loop iterates backward through the Roman numeral string (for (int i = strlen(roman) - 1; i >= 0; i--)
). - For each character, the program determines its corresponding decimal value using a
switch
statement.
Switch Statement:
- Converts Roman numeral characters to their decimal values:
I
= 1V
= 5X
= 10L
= 50C
= 100D
= 500M
= 1000
- If an invalid character is encountered, the function returns
-1
.
Adding or Subtracting Values:
- If the current value is less than the previous value (e.g., in the case of
IV
for 4), the program subtracts the current value from the total. - Otherwise, it adds the current value to the total.
Update Previous Value:
- After processing each character,
prevValue
is updated to the current value.
Main Function:
char roman[100];
: Declares an array to hold the input Roman numeral.printf("Enter a Roman numeral: ");
: Prompts the user for input.fgets(roman, sizeof(roman), stdin);
: Reads a line of text intoroman
.roman[strcspn(roman, "\n")] = 0;
: Removes the newline character added byfgets
.- Calls
romanToDecimal(roman)
to convert the input to decimal. - Outputs the result. If the conversion fails (i.e., invalid input), it prints an error message.
Return Statement:
return 0;
: Indicates successful execution of the program.
Input/Output Block:
Input:
- The user is prompted to enter a Roman numeral string.
Example Input:
Enter any Roman number: MCMXCIV
Output:
- The program outputs the decimal equivalent of the Roman numeral.
Example Output:
Its decimal value is : 1994
Conclusion:
The Roman numeral to decimal conversion program effectively translates Roman numeral strings into their decimal counterparts. It utilizes a straightforward algorithm to interpret numeral values and apply the rules governing their combinations. By implementing character checks and arithmetic operations, the program highlights key concepts in string processing and conditional logic in C. This functionality can be beneficial in educational tools, historical data analysis, and any application that involves numeral conversion, making it a valuable addition to a programmer’s toolkit.