← Back to Codes
C
Roman to Decimal Converter
Converts a Roman numeral string to its decimal integer equivalent.
roman_to_decimal.c
#include <stdio.h>
#include <string.h>
int main()
{
printf("INSTRUCTIONS: Enter a Roman numeral (e.g., XIV, MMXX) to convert to decimal.\n");
char rom[30];
int a[30], l, i, k, dec=0;
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);
return 0;
}Roman to Decimal Converter — Free C Code Example
Converts a Roman numeral string to its decimal integer equivalent. This free code example is available in C and can be copied and run immediately — no account or signup required. Use the language tabs above to switch between implementations.
How to Use This Code
- Select your language — click the language tab (C, Java, C++, or Python) above the code viewer.
- Copy the code — use the copy button in the top-right corner of the code block.
- Run it — paste into a file with the correct extension (.c, .java, .cpp, .py), compile, and run using your terminal or IDE.
- Follow the on-screen instructions in the terminal to provide any required inputs.
Frequently Asked Questions
What does this program do?
Converts a Roman numeral string to its decimal integer equivalent.
Which languages is it available in?
Available in C. Switch between implementations using the tabs at the top of the code viewer.
How do I run this code?
Copy the code, save it with the right file extension (.c, .java, .cpp, or .py), then compile or run it with your installed compiler or interpreter (GCC, JDK, G++, Python 3).
Is this code free?
Yes — free to view, copy, and use. No account required.