10+ C Roman to Decimal Converters

This post contains a total of 10+ Hand-Picked C Roman to Decimal Converter examples with source code. All the Roman to Decimal converters are made using C Programming Language.

You can use the source code of these programs for educational use with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Noor โค

Made by Noor โค. Simple program to convert roman number to decimal. Input : LXX Output : 70, please enter roman no in capital. ( Source )

#include <stdio.h>
#include<string.h>

int convert(char);

int main() {
    int i=0, num=0 ;
    char r[100];
    
    printf("enter roman number");
    scanf("%s",r);
    
   while(r[i])
   {
       if(convert(r[i])<0) //checking valadity
       {
           printf("\n Invalid Roman number");
           return 0;
       }
       
       if((strlen(r)-i)>2)  
       {
           if(convert(r[i])<convert(r[i+2]))//checking valadity
           {
               printf("\nInvalid Roman number ");
               return 0;
           }
       }
    
    //conversion of roman no into decimal

           if(convert(r[i])>=convert(r[i+1]))
           
           num = num + convert(r[i]);
           
           else
        {   
           num = num + (convert(r[i+1])-convert(r[i]));
           i++;
       }
       i++;
   }
   printf("\n Equivalent decimal number : %d",num);
    return 0;
}

int convert(char ch)
{
    int i=0;
    
    switch(ch)
    {
        case 'I':i=1;break;
        case 'V':i=5; break;
        case 'X':i=10;break;
        case 'L':i=50; break;
        case 'C':i=100;break;
        case 'D':i=500; break;
        case 'M':i=1000;break;
        case '\0':i=0; break;
        
        default:i=-1;
        
    }
    
    return i;
}

2. By Pringle

Made by Pringle. ( Source )

#include<stdio.h>

#include<string.h>

#include<stdlib.h>
int main()
{
int a[100],len,i,j,k;
char rom[100];
printf("Enter the Roman Numeral:\n");
scanf("%s",rom);
len=strlen(rom);
for(i=0; i<len; i++) 
{
 if(rom[i]=='I') a[i]=1;
 else if(rom[i]=='V') a[i]=5;
 else if(rom[i]=='X') a[i]=10;
 else if(rom[i]=='L') a[i]=50;
 else if(rom[i]=='C') a[i]=100;
 else if(rom[i]=='D') a[i]=500;
 else if(rom[i]=='M') a[i]=1000;
 else 
{
 printf("\nInvalid Value");
 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("Its Decimal Equivalent is:");
 printf("%d\n",k);
 return 0;
 
}
 

3. By Win Htay ๐Ÿ‡ฒ๐Ÿ‡ฒ

Made by Win Htay ๐Ÿ‡ฒ๐Ÿ‡ฒ. Roman to Decimal Converter, Test by giving Roman Number, You can use lower or upper letter. ( Source )

#include<stdio.h>
#include<string.h>

int GetValue(char c) 
{
	if(c=='i' || c=='I') return 1;
	else if(c=='v' || c=='V') return 5;
	else if(c=='x' || c=='X') return 10;
	else if(c=='l' || c=='L') return 50;
	else if(c=='c' || c=='C') return 100;
	else if(c=='d' || c=='D') return 500;
	else if(c=='m' || c=='M') return 1000;
    return 0;
}

int Roman2Dec(char st[])
{
	int n=0;
	for(int i=0; i<strlen(st); i++)
	{
		if(i==strlen(st)-1) 
		{ 
			n+= GetValue(st[i]); break;
		}
		int n1 = GetValue(st[i]);
		int n2 = GetValue(st[i+1]);
		if(n1 >= n2) 
			n += n1;
		else
		{	
			n += n2 - n1;
			i++;
		}
	}
	return n;
}
int main()
{
	char st[30]; 
	printf("Enter Roman Number : ");
	scanf("%s", st);
	int n = Roman2Dec(st);
	printf("%d", n);

	return 0;
}

4. By JothiRam

Made by JothiRam. ( Source )

 main()
{
   char rom[30];
   int a[30], l, i, k, dec;
  
   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);
}

5. By P S V L S SOWJANYA

Made by P S V L S SOWJANYA. ( Source )

#include<stdio.h>
#include<string.h>
 
int digit(char);
 
int main(){
 
    char roman_Number[1000];
    int i=0;
    long int number =0;

    scanf("%s",roman_Number);
 
    while(roman_Number[i]){
 
         if(digit(roman_Number[i]) < 0){
             printf("Invalid roman digit : %c",roman_Number[i]);
             return 0;
         }
 
         if((strlen(roman_Number) -i) > 2){
             if(digit(roman_Number[i]) < digit(roman_Number[i+2])){
                 printf("Invalid roman number");
                 return 0;
             }
         }
 
         if(digit(roman_Number[i]) >= digit(roman_Number[i+1]))
             number = number + digit(roman_Number[i]);
         else{
             number = number + (digit(roman_Number[i+1]) - digit(roman_Number[i]));
             i++;
         }
         i++;
    }
 
    printf("%ld",number);
 
    return 0;
 
}
 
int digit(char c){
 
    int value=0;
 
    switch(c){
         case 'I': value = 1; break;
         case 'V': value = 5; break;
         case 'X': value = 10; break;
         case 'L': value = 50; break;
         case 'C': value = 100; break;
         case 'D': value = 500; break;
         case 'M': value = 1000; break;
         case '\0': value = 0; break;
         default: value = -1; 
    }
 
    return value;
}

6. By Abhishek

Made by Abhishek. ( Source )

#include <stdio.h>
#include<string.h>
int main(){
char s1[4000];
long int res=0, n;
int i;
scanf("%s",s1);
n=strlen(s1);
for(i=0;i<n;i++){
    if(s1[i]=='I'){
        res+=1;
    }
    else if(s1[i]=='V'){
        res+=5;
        if(s1[i-1]=='I')
            res-=2;
    }
    else if(s1[i]=='X'){
        res+=10;
        if(s1[i-1]=='I')
            res-=2;
        else if(s1[i-1]=='V')
            res-=10;
    }
    else if(s1[i]=='L'){
        res+=50;
        if(s1[i-1]=='I')
            res-=2;
        else if(s1[i-1]=='V')
            res-=10;
        else if(s1[i-1]=='X')
            res-=20;
    }
    else if(s1[i]=='C'){
        res+=100;
        if(s1[i-1]=='I')
            res-=2;
        else if(s1[i-1]=='V')
            res-=10;
        else if(s1[i-1]=='X')
            res-=20;
        else if(s1[i-1]=='L')
            res-=100;
    }
    else if(s1[i]=='D'){
        res+=500;
        if(s1[i-1]=='I')
            res-=2;
        else if(s1[i-1]=='V')
            res-=10;
        else if(s1[i-1]=='X')
            res-=20;
        else if(s1[i-1]=='L')
            res-=100;
        else if(s1[i-1]=='C')
            res-=200;

    }
    else if(s1[i]=='M'){
        res+=1000;
        if(s1[i-1]=='I')
            res-=2;
        else if(s1[i-1]=='V')
            res-=10;
        else if(s1[i-1]=='X')
            res-=20;
        else if(s1[i-1]=='L')
            res-=100;
        else if(s1[i-1]=='C')
            res-=200;
        else if(s1[i-1]=='D')
            res-=1000;
    }
}
printf("%ld",res);
}

7. By Jamerock

Made by Jamerock. ( Source )

#include<stdio.h>
#include<string.h>


//Numerical string to number
int val(char r) {

//Roman Numeral input    
  if (r == 'I')

    return 1;

if (r == 'V')
return 5;

if (r == 'X')
return 10;

if (r == 'L')
return 50;

if (r == 'C')
return 100;

if (r == 'D')
return 500;

if (r == 'M')
return 1000;

    return -1;
}

//Roman Number To Decimal
int romannumToDecimal(char arr[]) {
    
int res = 0;

int i,len=strlen(arr);

for (i=0; i<len; i++) {
    
int s1 = val(arr[i]);

if (i+1 < len) {
    
int s2 = val(arr[i+1]);

if (s1 >= s2)
res = res + s1;

else
    
{
    
res = res + s2 - s1;

i++;

}

}

//repeat
else
    
{
res = res + s1;

i++;

}

}

return res;

}

//sub value
int subVal(char num1, char num2, int i, char *c) {
    
c[i++] = num1;

c[i++] = num2;

return i;

}

//
int value(char ch, int n, int i, char *c) {
    
int j;

for (j = 0; j < n; j++)
c[i++] = ch;

return i;
}

// Print Roman Numerals
   void printRoman(int num) {
   char c[10001];

  int i = 0;

  while (num != 0) {
    
if (num >= 1000) {
    
i = value('M', num/1000, i, c);

num = num%1000;

}

   else if (num >= 500)
    
{
    
if (num < 900) {
i = value('D', num/500, i, c);

num = num%500;

}
else
{
i = subVal('C', 'M', i, c);

num = num%100 ;

}

}

   else if (num >= 100) {
    
if (num < 400) {
    
i = value('C', num/100, i, c);

num = num%100;

}

else {
i = subVal('C','D',i,c);

num = num%100;

}

}

   else if (num >= 50 ) {
    
if (num < 90) {
    
i = value('L', num/50,i,c);

num = num%50;

}

     else {
i = subVal('X','C',i,c);

num = num%10;

}

}

else if (num >= 10)
    
{

if (num < 40) {
    
    
i = value('X', num/10,i,c);

num = num%10;

}

else
    
{
    
i = subVal('X','L',i,c);

num = num%10;

}

}

else if (num >= 5)
    
{
    
if (num < 9) {
    
i = value('V', num/5,i,c);

num = num%5;

}

else {
    
i = subVal('I','X',i,c);

num = 0;

}

}

else if (num >= 1) {
    
if (num < 4) {
    
i = value('I', num,i,c);

num = 0;

}

else {
    
i = subVal('I', 'V', i, c);

num = 0;

}

}

}

//
int j;

for (j = 0; j < i; j++)
    
printf("%c", c[j]);

}

//
    int main() {
    
    
  char arr1[100];

  char arr2[100];

scanf("%s %s",arr1,arr2);

int val1=romannumToDecimal(arr1); 

int val2=romannumToDecimal(arr2);

  printRoman(val1+val2);
 //Call input file and ouput file
 FILE *ifp, *ofp;
char *mode = "w";
char outputFilename[] = "ROMAN.TXT";

ifp = fopen("ROMAN.TXT", mode);

if (ifp == NULL) {
  fprintf(stderr, "Can't open input file ROMAN.TXT!\n");
  exit(1);
}

ofp = fopen(outputFilename, "w");

if (ofp == NULL) {
  fprintf(stderr, "Can't open output file %s!\n",
          outputFilename);
  exit(1);
}

fclose(ifp);
fclose(ofp);

    return 0;

}

8. By Caleb Brown

Made by CB93. An application written in C that converts roman numeral values into their equivalent decimal values. ( Source )

// Roman_Decimal.c

// Date: June 27, 2018

#define _CRT_SECURE_NO_WARNINGS
#define SIZE 10
#include < stdio.h > 
#include < ctype.h >



convertRoman(int * decimalNum, int * previousNum, int * bool);
checkPrevious(const int amount, int ** val, const int previousNum);

// Runs the Program
int main(void)
{
	int num = 0, decimalNum = 0, previousNum = 0, count, bool = 1, getch(void);

	printf("10 Roman numeral values will be input and converted into\n" 
		"their equivalent Hindu - Arabic numeric values.\n");

	for (count = 0; count < SIZE; count++) 
	{

		printf("\nInput Roman numeral #: %d  ", count + 1);

		convertRoman(&decimalNum, &previousNum, &bool);

	
		(bool ? printf(" = %d\n", decimalNum) : printf( 
			"Error - last character was not valid!!!\n")); 	// If TRUE
	

		decimalNum = 0, num = 0, previousNum = 0;
		bool = 1; // TRUE

	}
	printf("THATS ALL FOLKS :)");
	getch();

}


// Takes in roman numeral chars and converts into numeric values
convertRoman(int * decimalNum, int * previousNum, int * bool)
{
	int M = 1000, D = 500, C = 100, L = 50, X = 10, V = 5, I = 1;
	char input;
	while (((input) = getchar()) != '\n')
	{

		switch (toupper(input)) 
		{

		case 'I':
			*decimalNum += I; // Increment by 1
			checkPrevious(I, &decimalNum, *previousNum); *previousNum = I;
			break;

		case 'V':
			*decimalNum += V; // Increment by 5
			checkPrevious(V, &decimalNum, *previousNum); *previousNum = V;
			break;

		case 'X':
			*decimalNum += X; // Increment by 10
			checkPrevious(X, &decimalNum, *previousNum); *previousNum = X;
			break;

		case 'L': 
			*decimalNum += L; // Increment by 50
			checkPrevious(L, &decimalNum, *previousNum); *previousNum = L;
			break;

		case 'C':
			*decimalNum += C; // Increment by 100
			checkPrevious(C, &decimalNum, *previousNum); *previousNum = C;
			break;

		case 'D':
			*decimalNum += D; // Increment by 500
			checkPrevious(D, &decimalNum, *previousNum); *previousNum = D;
			break;

		case 'M':
			*decimalNum += M; // Increment by 1000
			checkPrevious(M, &decimalNum, *previousNum); *previousNum = M;
			break;

		default:
			* bool = 0; // FALSE
			break;

		}
	}

}

// Detects when to apply subtractive notation from passed previousChar and currentChar
checkPrevious(const int currentChar, int ** val, const int previousChar) 
{

	if (previousChar < currentChar) 
	{
		** val -= (2 * previousChar);
	}

}

9. By Ramongr

Made by Ramongr. Transforms roman numerals to decimal value. ( Source )

/*
####################################################################
GLOBAL INCLUDES
####################################################################
*/

#include <stdio.h>
#include <string.h>

/*
####################################################################
CODE
####################################################################
*/

//This function assumes the string is in the correct roman form and in CAPS
int
roman_to_dec(char *roman){
    int i,sum;
    char letter='I';

    i=0;
    while(roman[i++])

    sum = 0;

    while(i>=0){

        switch(roman[i]){
            case 'I':
                if(letter == 'V'){
                    sum -=1;
                }else{
                    sum +=1;
                }
            break;

            case 'V':
                if(letter == 'X'){
                    sum -=5;
                }else{
                    sum +=5;
                    letter = 'V';
                }
            break;

            case 'X':
                if(letter == 'L'){
                    sum -=10;
                }else{
                    sum +=10;
                    letter = 'X';
                }
            break;

            case 'L':
                if(letter == 'C' || letter == 'D'){
                    sum -=50;
                }else{
                    sum +=50;
                    letter = 'L';
                }
            break;

            case 'C':
                if(letter == 'D' || letter == 'M'){
                    sum -=100;
                }else{
                    sum +=100;
                    letter = 'C';
                }
            break;

            case 'D':
                if(letter == 'M'){
                    sum -=500;
                }else{
                    sum +=500;
                    letter = 'D';
                }
            break;

            //There are bigger numbers, but are not supported by ASCII
            case 'M':
                sum +=1000;
                letter = 'M';
            break;
        }

        i--;
    }

    printf("%d\n", sum);
}

int
main(int argc, char **argv){

    roman_to_dec(argv[1]);

    return 0;
}

10. By Harjas Dot

Made by Harjas Dot. A ‘C’ program to convert Roman numerals to Decimal expression and vice versa. ( Source )

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char arr[],val[10];
int a,x=0,l,c=0,b,d,e=0,f=0,g,h=1;
int test(),result(),vidx();

int main()
{
  while(h!=0)
  {
    printf("\nIF YOU WANT TO CONVERT DECIMAL TO ROMAN PRESS 1\n");
    printf("IF YOU WANT TO CONVERT ROMAN TO DECIMAL PRESS 2\n");
    printf("IF YOU WNT TO EXIT PRESS 3\n");
    scanf("%d",&b);
    if(b==1 || b==2)
    {
      vidx();
    }
    else
    {
      h=0;

    }
  }
  printf("THANK YOU!!!!");






    return 0;
}


//---------------
vidx()
{
  if(b==1){
    printf("YOUR NUMBER MUST BE LESS THAN 4000 \n");
    printf("Enter the decimal number: ");
    scanf("%d",&a);
    while (a!=0)
    {
        test();
        //printf("%d",a);
    }
    //printf("%d",c);
    result();
    memset(arr,0,c);
  }
  else
  {
    printf("ENTER THE ROMAN EXPRESSION IN CAPITAL\n");
    scanf("%s",val);
    for(d=0;d<10;d+=1)
    {
      f=val[d];
      if(f=='I')
      {
        d+=1;
        g=val[d];
        if(g=='I'){
          e+=2;
        }
        else if(g=='V'){
          e+=4;
        }
        else if(g=='X'){
          e+=9;
        }
        else
        {
          e+=1;
          d-=1;
        }
      }
//-----------------------------------------------------------------------
      else if(f=='V')
      {
        e+=5;
      }
      else if(f=='X')
      {
        d+=1;
        g=val[d];
        if(g=='L')
        {
          e+=40;
        }
        else if(g=='C'){
          e+=90;
        }
        else
        {
          e+=10;
          d-=1;
        }
      }
      else if(f=='L')
      {
         e+=50;
       }
       else if(f=='C')
       {
         d+=1;
         g=val[d];
         if(g=='D'){
           e+=400;
         }
         else if(g=='M'){
           e+=900;

         }
         else{
           e+=100;
           d-=1;
         }
       }
       else if(f=='D'){
        e+=500;
       }
       else if(f=='M'){
         e+=1000;
       }
       }
      }

    printf("\n%d",e);
    memset(val,0,10);
    e=0;
}


//-------------------



test()
{
    if(a>=1 && a<5)
    {
      if(a==4)
      {
        arr[x]='i';
        c+=1;
        x+=1;
        arr[x]='v';
        c+=1;
        x+=1;
        a-=4;
      }
      else
      {
        arr[x]='i';
        c+=1;
        x+=1;
        a-=1;
      }

    }
    else if(a>=5 && a<10)
    {

        if(a==9)
        {
            arr[x]='I';
            c+=1;
            x+=1;

            arr[x]='X';
            c+=1;
            x+=1;

            a-=9;

        }
        else
        {
          arr[x]='V';
          c+=1;
          x+=1;
          a-=5;
        }

    }
    else if(a>=10 && a<50)
    {
      if(a>=40){
        arr[x]='X';
        c+=1;
        x+=1;
        arr[x]='L';
        c+=1;
        x+=1;
        a-=40;
      }
      else{
        arr[x]='X';
        c+=1;
        x+=1;
        a-=10;
      }
    }
    else if(a>=50 && a<100){
      if(a>=90){
        arr[x]='X';
        c+=1;
        x+=1;
        arr[x]='C';
        c+=1;
        x+=1;
        a-=90;

      }
      else{
        arr[x]='L';
        x+=1;
        c+=1;
        a-=50;
      }
    }
    else if(a>=100 && a<500){
      if(a>=400){
        arr[x]='C';
        x+=1;
        c+=1;
        arr[x]='D';
        x+=1;
        c+=1;
        a-=400;
      }
      else{
        arr[x]='C';
        c+=1;
        x+=1;
        a-=100;
      }
    }
    else if(a>=500 && a<1000){
      if(a>=900){
        arr[x]='C';
        c+=1;
        x+=1;
        arr[x]='M';
        c+=1;
        x+=1;
        a-=900;
      }
      else{
        arr[x]='D';
        x+=1;
        c+=1;
        a-=500;
      }
    }
    else if(a>=1000 && a<4000){
      arr[x]='M';
      c+=1;
      x+=1;
      a-=1000;
    }
    else{
      printf("U ENTERED A NUMBER WHICH IS MORE THAN OR EQUAL TO 4000 ");
      a-=a;
    }
}
result()
{
    for(l=0;l<c;l=l+1)
    {
        printf("%c",arr[l]);
    }


}

11. By PRASADsiva

Made by PRASADsiva. ( Source )

#include<stdio.h>
#include<string.h>
 
int d(char);
 
int main(){
 
    char rN[1000];
    int i=0;
    long int n =0;
    scanf("%s",rN);
 
    while(rN[i]){
 
         if(d(rN[i]) < 0){
             printf("Invalid roman digit : %c",rN[i]);
             return 0;
         }
 
         if((strlen(rN) -i) > 2){
             if(d(rN[i]) < d(rN[i+2])){
                 printf("Invalid roman number");
                 return 0;
             }
         }
 
         if(d(rN[i]) >= d(rN[i+1]))
             n = n + d(rN[i]);
         else{
             n = n + (d(rN[i+1]) - d(rN[i]));
             i++;
         }
         i++;
    }
 
    printf("%ld",n);
 
    return 0;
 
}
 
int d(char c){
 
    int value=0;
 
    switch(c){
         case 'I': value = 1; break;
         case 'V': value = 5; break;
         case 'X': value = 10; break;
         case 'L': value = 50; break;
         case 'C': value = 100; break;
         case 'D': value = 500; break;
         case 'M': value = 1000; break;
         case '\0': value = 0; break;
         default: value = -1; 
    }
 
    return value;
}