# C Program to Convert Roman to number

Problem: Write a C program to convert roman number into decimal equivalent.

Solution:
1. Accept a roman number as input.
2. Define value of each roman digit using switch statement.
3. Through switch statement access each digit of a roman number and compute the value.
4. Finally display the the value.

## C program to convert roman number to decimal number

``````/* Aim: C Program to Convert Roman Number to Decimal Number */

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

int digit(char); // Function Prototype

int main()
{
char roman_Number[1000];
int i=0;
long int decimal_number =0;

printf("\n Enter any roman number (Valid digits are I, V, X, L, C, D, M):- ");
scanf("%s",roman_Number);

while(roman_Number[i])
{
if(digit(roman_Number[i]) < 0)
{
printf("\n Invalid roman digit : %c \n",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]))
decimal_number = decimal_number + digit(roman_Number[i]);
else
{
decimal_number = decimal_number + (digit(roman_Number[i+1]) - digit(roman_Number[i]));
i++;
}

i++;
}

printf("\n Its decimal value is : %ld \n",decimal_number);

return 0;
}

int digit(char ch)
{
int value=0;

switch(ch)
{
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;
}

/* Example Output of Above Code:-

Enter any roman number (Valid digits are I, V, X, L, C, D, M):
XV
Its decimal value is: 15

*/``````