- C program to test if a number is a power of 2
- C program to multiply two number without using multiplication(*) operator
- C program to display Fibonacci series
- C program to compare two numbers without using relational operators
- C program to convert roman number into decimal number
- C program to encrypt and decrypt the string
- C program to make calculator using switch case
- C program to convert temperature from degree celsius to fahrenheit
- Selection sort in C programming
- Binary search in C programming
- C program to sort array using bubble sort algorithm
- Linear Search in C Programming – Program and Explanation
- C program to swap two numbers
- C program to find LCM of two numbers
- C program to find GCD of two numbers
- C program to find LCM and GCD using recursion
- C program to display prime numbers between two numbers using function and without using function
- C program to check whether a string is palindrome or not
- C program to read a character from keyboard and print it in reverse case i.e if input is lower case output will be upper case and vice versa
- C program to check whether a number is prime or not
- C program to check whether a number is positive, negative or zero
- C program to check whether a number is palindrome or not
- C program to check leap year
- C program to check Armstrong number
- C program to calculate the area of square with and without using function
- C program to calculate mean using arrays
- C program to calculate area of circle

In this example, you will in depth about C program to convert roman number into decimal number with proper explanation.

–**Roman numbers**

Roman numbers are represented by the following characters:

I, V, X, L, C, D, M

Before writing the code, you should understand how roman number work and their rules:

Repeating a roman numeral up to three times represents the addition of the number whereas, V, L, and D cannot be repeated.

III is equivalent to (1+1+1 = 3) XXX is equivalent to (10+10+10 = 30)

If roman numbers are in decreasing order from left to right it represents an addition of the numbers.

LX is equivalent to (50+10 = 60)

Writing the smaller number to the left of the larger number in the Roman system represents subtraction.

XL is equivalent to (10-50 = 40) CD is equivalent to (500-100 = 400)

You will need to have sound knowledge of following articles for proper understanding.

Now, let’s write C code to convert the roman number into the decimal number using above rules.

```
//C program to convert roman numbers into decimal
#include <stdio.h>
#include <string.h>
int convert(char); //function prototype of conversion function
int main ()
{
int i = 0, num = 0;
char romanNumber[100]; //array for storing roman number
printf("Enter Roman number: ");
scanf("%s", romanNumber);
while(romanNumber[ i ])
{
//if condition for checking validity of roman number
if(convert(romanNumber[ i ]) < 0 )
{
printf("\nInvalid Roman Number.\n");
return 0;
}
//if condition for checking validity of roman number
if((strlen(romanNumber) - i ) > 2)
{
if(convert(romanNumber[ i ]) < convert(romanNumber[i + 2]))
{
printf("\nInvalid Roman Number.\n");
return 0;
}
}
//if condition for converting roman number into decimal number
if(convert(romanNumber[ i ]) >= convert(romanNumber[i + 1])) //true is first roman number is greater or equal to second
num = num + convert(romanNumber[ i ]);
else
{
num = num + (convert(romanNumber[i + 1]) - convert(romanNumber[ i ]));
i++;
}
i++;
}
//displaying converted number
printf("\nEquivalent decimal number: %d\n", num);
return 0;
}
//converting roman number into equivalent decimal value
int convert(char ch)
{
int value = 0;
switch(ch)
{
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;
}
```

[adsense1]

**Output**

#1

#2

#3

Explanation

In the above program, we have included `string.h`

library to use standard string library function `strlen`

that is used to determine the length of the roman number.

`while`

function is used to access every digit in the Roman number that is stored in the character array `romanNumber`

.

The program first checks for the validity of the roman number entered by the user using two consecutive `if`

conditions as discussed in the rule above.

If the entered roman number is valid then only third `if`

condition computes the equivalent decimal number.