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.