Symbol
|
Value
|
I
|
1
|
V
|
5
|
X
|
10
|
L
|
50
|
C
|
100
|
D
|
500
|
M
|
1000
|
Each character in a Roman numeral stands for the corresponding value. IN the simplest case, the value of the Roman numeral as a whole is the sum of the individual character values given in the table. For example, the string "LXXXVII" denotes 50 + 10 + 10 + 10 + 5 + 1 + 1, or simply, 87.
In this post, I will discuss creating a program which would convert Roman Numerals to Hindu-Arabic Numerals (or our simple integers).
This program will contain two functions, (1) the main function (default), and (2) the decimal function. The main function, contains our user input, a for-loop - which will process each roman numeral character, and the finally the program output. The decimal function, works in line with our main function's for-loop. This function is responsible for returning the corresponding decimal (integer) value of each roman numeral (character) passed to it.
1. Main function:
Let us first declare our variables. In our main function we will be using three variables. The first variable is a string datatype. But as we all know, we don't have a 'string' data type in C, instead we have a Char data type (char array) instead. Let us use that. In our program, we will only limit our string's total length to 15. Let us name our variable as roman, this variable will contain the roman numeral which will be asked from the user later on.
char roman[15];
The next variables we have to declare are integers i and deci. Variable i will be the counter for the loop we will use later on in our program. Variable deci will contain the decimal (integer) value of our roman numeral.
int i, deci=0;
Next, is asking the user for our roman numeral value,
printf("Input Roman Numeral : ");
scanf("%s",roman);
And lastly, the for-loop:
The conditions for our loop are as follows: counter variable i is instantiated at 0, while i's value is less than the string length of variable roman, variable i gets incremented at the end of the loop's run.
for (i=0; i<strlen(roman);i++)
The body of this loop will contain an if-else statement which will evaluate each (and the succeeding) roman numeral character.
The if condition will state that if the decimal value of the current character (roman[i]) is less than the decimal value of the succeeding character (roman[i+1]), then the decimal value of the current character (roman[i]) will be deducted from the current value of deci. This condition is done, in order to accommodate roman numerals like IV (4), CD (400), CM (900), IX (9), without this condition, these numerals will result be converted incorrectly (5, 600, 1100, and 10 respectively).
While else will obviously accommodate the opposite condition of the one and only if statement. Here, current value of variable deci will be added with the integer returned from the decimal function.
Take note that we have to pass the current array location of the variable roman to decimal function's parameters. Also note, that the roman[i] variable/array location is enclosed within the toupper function, which converts any character to its capital / uppercase form.
for (i=0; i<strlen(roman);i++)
{
if((decimal(toupper(roman[i])))<(decimal(toupper(roman[i+1]))))
deci-=decimal(toupper(roman[i]));
else
deci+=decimal(toupper(roman[i]));
}
2. Decimal Function:
The decimal function has an integer data type, because it will return an integer as it finishes its processes. Since this function receives a character from main (roman[i]), a character variable should be declared in its parameters. Let us name this variable as variable a.
int decimal(char a)
Next, is to declare an integer variable which will contain the value of the roman numeral character to be converted, name the variable as variable value, with the initial value of 0.
int value = 0;
At this point, a switch-case function will be used. In the parameter of switch, place the variable a. For each case, place the Roman Numeral (letter), and assign its corresponding value to the variable value. Don't forget to break; each case.
switch(a)
{
case 'M': value = 1000;
break;
case 'D': value = 500;
break;
case 'C': value = 100;
break;
case 'L': value = 50;
break;
case 'X': value = 10;
break;
case 'V': value = 5;
break;
case 'I': value = 1;
break;
}
And finally, return the current value of variable value.
return value;
-----------
Here is sample input, and output of the program:

Here is the source code:
#include <stdio.h>
int decimal(char a)
{
int value = 0;
switch(a)
{
case 'M': value = 1000;
break;
case 'D': value = 500;
break;
case 'C': value = 100;
break;
case 'L': value = 50;
break;
case 'X': value = 10;
break;
case 'V': value = 5;
break;
case 'I': value = 1;
break;
}
return value;
}
int main()
{
char roman[15];
int i;
int deci=0;
printf("Input Roman Numeral : ");
scanf("%s",roman);
for (i=0; i<strlen(roman);i++)
{
if((decimal(toupper(roman[i])))<(decimal(toupper(roman[i+1]))))
deci-=decimal(toupper(roman[i]));
else
deci+=decimal(toupper(roman[i]));
}
printf("%d is the equivalent of %s",deci,roman);
return 0;
}