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);
}
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: