Write a C program to convert a Roman numeral to its decimal equivalent.

Algorithm:

Step 1: Start
Step 2: read the roman numerical as string
Step 3: find length of roman numerical
Step 4: for each charcter in the string
             i) if(char = I) then decimal = 1
             ii) if(char = V) then decimal = 5
             iii) if(char = X) then decimal = 10
             iv) if(char = L) then decimal = 50
             v) if(char = C) then decimal = 100
             vi) if(char = D) then decimal = 500
             vii) if(char = M) then decimal = 1000
             viii) otherwise invalid character
Step 5: repeat step 4 until the length of the string
Step 6: k = char[length - 1]
Step 7: for each character of decimal string
           i) if(decimal[i] > dec[i - 1]) then k = k - decimal[i - 1]
           ii) else if(decimal[i] = decimal[i - 1 or decimal[i] < decimal[i - 1) then k = k + decimall[i - 1]
Step 8: repate step 7 until the length of decimal string
Step 9: print decimal value
Step 10: Stop

Program:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
   char rom[30];
   int a[30], l, i, k, dec;
   clrscr();
   printf("Enter the roman number\n");
   scanf("%s", &rom);
   l =strlen(rom);
   for(i = 0; i < l; i++)
   {
      switch (rom[i])
      {
     case 'I':  a[i] = 1;
            break;
     case 'V':  a[i] = 5;
            break;
     case 'X':  a[i] = 10;
            break;
     case 'L':  a[i] = 50;
            break;
     case 'C':  a[i] = 100;
            break;
     case 'D':  dec = dec + 500;
            break;
     case 'M':  a[i] = 1000;
            break;
     default :  printf("Invalid choice");
            break;
      }
   }
   k = a[l - 1];
   for(i = l - 1; i > 0; i--)
   {
      if(a[i] > a[i - 1])
      {
     k = k - a[i - 1];
      }
      if(a[i] <= a[i - 1])
      {
     k = k + a[i - 1];
      }
   }
   printf("decimal equivalent is %d", k);
   getch();
}

Input & Output:

Enter the roman number 
XIV
Decimal equivalent is 14