Computer >> Computer tutorials >  >> Programming >> C programming

C program to convert roman numbers to decimal numbers


Given below is an algorithm to convert roman numbers to decimal numbers in C language −

Algorithm

Step 1 − Start

Step 2 − Read the roman numeral at runtime

Step 3 − length: = strlen(roman)

Step 4 − for i = 0 to length-1 do

      Step 4.1 − switch(roman[i])

          Step 4.1.1 − case ‘m’:

          Step 4.1.2 − case ‘M’:

               Step 4.1.2.1 − d[i]: =1000

          Step 4.1.3 − case ‘d’:

               Step 4.1.4 − case ‘D’:

          Step 4.1.4.1 − d[i]: =500

          Step 4.1.5 − case ‘c’:

               Step 4.1.6 − case ‘C’:

                   Step 4.1.6.1 − d[i]: =100

          Step 4.1.7 − case ‘l’:

          Step 4.1.8 − case ‘L’:

                   Step 4.1.8.1 − d[i]: =50

          Step 4.1.9 − case ‘x’:

          Step 4.1.10 − case ‘X’:

                   Step 4.1.10.1 − d[i]: =10

           Step 4.1.11 − case ‘v’:

          Step 4.1.12 − case ‘V’:

                Step 4.1.12.1 − d[i]: =5

          Step 4.1.13 − case ‘i’:

          Step 4.1.14 − case ‘I’:

                Step 4.1.14.1 − d[i]: =1

          Step 5 − for i =0 to length-1 do

                Step 5.1 − if (i==length-1) or (d[i]>=d[i+1]) then

                      Step 5.1.1 − deci += d[i]

          Step 5.2 − else

              Step 5.2.1 − deci -= d[i]

Step 6 − Print the decimal equivalent of roman numeral

Step 7 − Stop

Program

Following is the C program to convert roman numbers to decimal numbers

#include <stdio.h>
#include <conio.h>
main(){
   char roman[30];
   int deci=0;
   int length,i,d[30];
   printf("The Roman equivalent to decimal\n");
   printf("Decimal:.........Roman\n");
   printf("%5d............%3c\n",1,'I');
   printf("%5d............%3c\n",5,'V');
   printf("%5d............%3c\n",10,'X');
   printf("%5d............%3c\n",50,'L');
   printf("%5d............%3c\n",100,'C');
   printf("%5d............%3c\n",500,'D');
   printf("%5d............%3c\n",1000,'M');
   printf("Enter a Roman numeral:");
   scanf("%s",roman);
   length=strlen(roman);
   for(i=0;i<length;i++){
      switch(roman[i]){
         case 'm':
         case 'M': d[i]=1000; break;
         case 'd':
         case 'D': d[i]= 500; break;
         case 'c':
         case 'C': d[i]= 100; break;
         case 'l':
         case 'L': d[i]= 50; break;
         case 'x':
         case 'X': d[i]= 10; break;;
         case 'v':
         case 'V': d[i]= 5; break;
         case 'i':
         case 'I': d[i]= 1;
      }
   }
   for(i=0;i<length;i++){
      if(i==length-1 || d[i]>=d[i+1])
         deci += d[i];
      else
         deci -= d[i];
   }
   printf("The Decimal equivalent of Roman numeral %s is %d", roman, deci);
}

Output

When the above program is executed, it produces the following result −

The Roman equivalent to decimal
Decimal:.........Roman
1............ I
5............ V
10............ X
50............ L
100............ C
500............ D
1000............ M
Enter a Roman numeral: M
The Decimal equivalent of Roman Numeral M is 1000