Sai A
Updated date Nov 04, 2023
In this blog, we will learn how to convert Roman numerals to decimal numbers in C. Explore two distinct methods like Iterative approach and recursive approach.

## Introduction:

Roman numerals, an ancient numeral system that originated in Rome, are still occasionally used today, primarily in contexts such as clock faces, book chapters, and the numbering of movie sequels. While they are interesting and carry historical significance, converting them to decimal numbers can be a complex task.

In this blog post, we will explore two efficient methods for converting Roman numerals to their decimal equivalents using the C programming language. We will provide a detailed explanation of each method and then demonstrate their implementation with a C program. The output of the program will confirm the accuracy of our conversions.

## Method 1: Iterative Approach

The first method we will explore is an iterative approach, which involves scanning the Roman numeral from left to right. As we scan each character, we compare it to the following character to determine whether we need to add or subtract its value from the result.

``````#include <stdio.h>

// Function to convert Roman numerals to decimal
int romanToDecimal(char *roman) {
int result = 0;

// Define a dictionary of Roman numerals and their corresponding values
int romanValues[26];
romanValues['I' - 'A'] = 1;
romanValues['V' - 'A'] = 5;
romanValues['X' - 'A'] = 10;
romanValues['L' - 'A'] = 50;
romanValues['C' - 'A'] = 100;
romanValues['D' - 'A'] = 500;
romanValues['M' - 'A'] = 1000;

for (int i = 0; roman[i]; i++) {
// Get the value of the current Roman numeral
int value = romanValues[roman[i] - 'A'];

// If the next numeral is larger, subtract the current value
if (roman[i + 1] && romanValues[roman[i + 1] - 'A'] > value) {
result -= value;
} else {
result += value;
}
}

return result;
}

int main() {
char roman[] = "MCMXCIV"; // Example Roman numeral

int decimal = romanToDecimal(roman);

printf("Roman numeral: %s\n", roman);
printf("Decimal equivalent: %d\n", decimal);

return 0;
}
``````

### Output:

``````Roman numeral: MCMXCIV
Decimal equivalent: 1994
``````

In this example, the Roman numeral "MCMXCIV" is converted to the decimal equivalent "1994" using the iterative approach. The program successfully performs the conversion and produces the expected output.

## Method 2: Recursive Approach

The second method we will explore is a recursive approach. This method involves dividing the Roman numeral into smaller segments and recursively calculating the decimal values of those segments until the entire numeral is converted.

``````#include <stdio.h>

// Function to convert Roman numerals to decimal
int romanToDecimalRecursive(char *roman) {
if (*roman == '\0') {
return 0; // Base case: empty numeral
}

// Define a dictionary of Roman numerals and their corresponding values
int romanValues[26];
romanValues['I' - 'A'] = 1;
romanValues['V' - 'A'] = 5;
romanValues['X' - 'A'] = 10;
romanValues['L' - 'A'] = 50;
romanValues['C' - 'A'] = 100;
romanValues['D' - 'A'] = 500;
romanValues['M' - 'A'] = 1000;

// Get the value of the current Roman numeral
int value = romanValues[roman[0] - 'A'];

// If the next numeral is larger, subtract the current value
if (roman[1] && romanValues[roman[1] - 'A'] > value) {
return romanValues[roman[1] - 'A'] - value + romanToDecimalRecursive(roman + 2);
} else {
return value + romanToDecimalRecursive(roman + 1);
}
}

int main() {
char roman[] = "MCMXCIV"; // Example Roman numeral

int decimal = romanToDecimalRecursive(roman);

printf("Roman numeral: %s\n", roman);
printf("Decimal equivalent: %d\n", decimal);

return 0;
}
``````

### Output:

``````Roman numeral: MCMXCIV
Decimal equivalent: 1994
``````

In this example, the same Roman numeral "MCMXCIV" is converted to the decimal equivalent "1994" using the recursive approach. The program successfully performs the conversion and produces the expected output.

## Conclusion:

In this blog, we have explored two efficient methods for converting Roman numerals to their decimal equivalents using the C language. The first method is an iterative approach that scans the Roman numeral from left to right and makes decisions based on the values of adjacent characters. The second method is a recursive approach that divides the Roman numeral into smaller segments and recursively calculates their values.