Write a C program to convert a Roman numeral to its decimal equivalent.
Program Code:
#include<stdio.h>
#include<string.h>
void main()
{
char a[10];
int total[10],sum=0,i,l;
printf("\n enter a roman number: ");
gets(a);
l=strlen(a);
for(i=0;i<l;i++)
{
switch(a[i])
{
case 'M':total[i]=1000;
break;
case 'D':total[i]=500;
break;
case 'C':total[i]=100;
break;
case 'L':total[i]=50;
break;
case 'X':total[i]=10;
break;
case 'I':total[i]=1;
break;
}
sum=sum+total[i];
}
for(i=0;i<l-1;i++)
{
if(total[i]<total[i+1])
{
sum=sum-2*total[i];
}
printf("\n the decimal equivalent is %d",sum);
}
}
Post A Comment:
0 comments: