C program to convert Roman Number to Decimal Number

Mr Coder November 5, 2012 3

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

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 numbers 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 Roman Number to Decimal number 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
  {
printf("\nPlease enter correct roman numeral.");
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.

3 Comments »

  1. Mr Coder November 6, 2012 at 9:38 pm - Reply

    testing captcha..

  2. Lokesh November 6, 2012 at 9:39 pm - Reply

    Testing revisited.

  3. srinath reddy August 3, 2013 at 4:45 am - Reply

    very good program. Thanks for sharing.

Leave A Response »