C program to convert Roman Number to Decimal Number

# C program to convert Roman Number to Decimal Number

15425
10
SHARE

C program to convert Roman Number to Decimal Number : In my previous code, we have learned how to write a to convert Decimal number to Roman Number. Today we will learn how to write a .

Roman numerals, the numeric system in ancient Rome, uses combinations of letters from the Latin alphabet to signify values. There are several ways of representing the roman say 4 decimal number can be represented as IV or IIII. But as per standardization of Roman Numbers IV is valid and accepted by Mathematicians as better way to represent it.  Cost cutting is always being a fashion and been adapted by all, same happens in case of Roman Numbers. Lesser number of digits representation is considered as better.

The Romans used a special method of showing numbers, based on the following symbols:

 1 5 10 50 100 500 1000 I V X L C D M

.

Now there are few rules that are being followed by modern age representatives :

1. A number written in  Decimal numerals can be broken into digits. For example, 1903 is composed of 1, 9, 0, and 3. To write the Roman numeral, each of the non-zero digits should be treated separately. In the above example, 1,000 = M, 900 = CM, and 3 = III. Therefore, 1903 = MCMIII.

2. The symbols “I”, “X”, “C”, and “M” can be repeated three times in succession, but no more. (They may appear more than three times if they appear non-sequentially, such as XXXIX.) “D”, “L”, and “V” can never be repeated.

3. “I” can be subtracted from “V” and “X” only. “X” can be subtracted from “L” and “C” only. “C” can be subtracted from “D” and “M” only. “V”, “L”, and “D” can never be subtracted.

Numbers greater than 1,000 are formed by placing a dash over the symbol, meaning “times 1,000”, but these are not commonly used:

 5,000 10,000 50,000 100,000 500,000 1,000,000 _ V _ X _ L _ C _ D _ M

.

I have also avoided the same in the below C program. So there is one conflict in below program that it repeats M as many as times the thousands are there.

By considering all above rule, let us see how to write a C program to convert which take roman number as input of the program and displays the equivalent Decimal number.

Platform used : C Language

Compiler Used : Dev C++

## C program to convert Roman Number to Decimal Number :

```#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
int main(void)
{
int a[20],len,i=0,j,k;
char roman[2];
printf("Enter the Roman Numeral(Note: Only use alphabets I,V,X,L,C,D,M): ");
scanf("%s",roman);
len=strlen(roman);
for(i=0;i<len;i++)
{
if(roman[i]=='I')
a[i]=1;
else if(roman[i]=='V')
a[i]=5;
else if(roman[i]=='X')
a[i]=10;
else if(roman[i]=='L')
a[i]=50;
else if(roman[i]=='C')
a[i]=100;
else if(roman[i]=='D')
a[i]=500;
else if(roman[i]=='M')
a[i]=1000;
else
{
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<a[i-1])
k=k+a[i-1];
}
printf("\nEquivalent Decimal Number is %d ",k);
getch();
return 0;
}```

We hope you all have enjoyed the C code to convert Roman number into decimal number. If you have any issues with code or logic ask us in form of comments.

SHARE
Previous articleC program to Convert Decimal Number into Roman Number
Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games. Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com

1. This programs works fine if the values entered are correct but what is someone enters something like XXXXV or LMV which are wrong. How To Handle these situations.

2. /*This program Supports any Roman Numericals to Decimal Number:*/

#include
#include

int main()
{
int i,j,lf,rt,s=0,flag=0,N[7]={1,5,10,50,100,500,1000};
char R[7]={‘I’,’V’,’X’,’L’,’C’,’D’,’M’},*S;
S=malloc(256);
printf(“\nEnter Roman Numerical’s: “);
scanf(“%s”,S);
printf(“\nEntered Roman Numerical :%s “,S);
for(j=0;S[j];j++)
{
for(i=0;i<7;i++)
{
if(R[i]==S[j])
{
lf=i;
break;
}
}
for(i=0;iN[rt])||(lf==rt)||(rt==-1))&&(flag<2))
{
s+=N[lf];
}
else if((N[lf]<N[rt])&&(rt<3)&&(S[j+2]=='\0'))
{
s+=N[rt]-N[lf];
j++;
}
else
{
printf("\n\nEnter Valid Roman Numerical Number\n");
return 1;
}
rt=lf=-1;
}
printf("\nRoman's Equivalent Number : %d\n",s);
return 0;
}

3. /*sorry ignore the above one,*/

#include
#include

int main()
{
int i,lf,rt,j,s=0,flag=0,N[7]={1,5,10,50,100,500,1000};
char R[7]={‘I’,’V’,’X’,’L’,’C’,’D’,’M’},*S;
S=malloc(256);
printf(“\nEnter Roman Numerical’s: “);
scanf(“%s”,S);
printf(“\nEntered Roman Numerical :%s “,S);
for(j=0;S[j];j++)
{
for(i=0;i<7;i++)
{
if(R[i]==S[j])
{
lf=i;
break;
}
}
for(i=0;iN[rt])||(lf==rt)||(rt==-1))&&(flag<2))
{
s+=N[lf];
}
else if((N[lf]<N[rt])&&(rt<3)&&(S[j+2]=='\0'))
{
s+=N[rt]-N[lf];
j++;
}
else
{
printf("\n\nEnter Valid Roman Numerical Number\n");
return 1;
}
rt=lf=-1;
}
printf("\nRoman's Equivalent Number : %d\n",s);
return 0;
}