Roman Numeral Converter in C
This program provides functions to convert numbers to Roman numerals and vice versa. It includes two main functions:
intToRoman(int num, char *roman)
: Converts an integer to a Roman numeral string.romanToInt(char *roman)
: Converts a Roman numeral string to an integer.
Program Structure
The program is divided into several parts:
- Header files and constants
- Function prototypes
- Main conversion functions
- Main function to demonstrate usage
Code
#include <stdio.h>
#include <string.h>
#define MAX_ROMAN_NUMERAL_LENGTH 20
// Function prototypes
void intToRoman(int num, char *roman);
int romanToInt(const char *roman);
// Main conversion functions
void intToRoman(int num, char *roman) {
// Arrays of Roman numerals and corresponding values
const char *romanNumerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
const int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
int i = 0;
roman[0] = '\0'; // Initialize the output string
while (num > 0) {
while (num >= values[i]) {
strcat(roman, romanNumerals[i]);
num -= values[i];
}
i++;
}
}
int romanToInt(const char *roman) {
// Arrays of Roman numerals and corresponding values
const char *romanNumerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
const int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
int num = 0;
int i = 0;
int len = strlen(roman);
while (i < len) {
for (int j = 0; j < 13; j++) {
int romanNumeralLen = strlen(romanNumerals[j]);
if (strncmp(roman + i, romanNumerals[j], romanNumeralLen) == 0) {
num += values[j];
i += romanNumeralLen;
break;
}
}
}
return num;
}
// Main function to demonstrate usage
int main() {
char roman[MAX_ROMAN_NUMERAL_LENGTH];
int number = 1987;
// Convert integer to Roman numeral
intToRoman(number, roman);
printf("%d in Roman numerals is %s\n", number, roman);
// Convert Roman numeral back to integer
int convertedNumber = romanToInt(roman);
printf("%s in integer is %d\n", roman, convertedNumber);
return 0;
}
Explanation
Header Files and Constants
We include the necessary header files <stdio.h>
for input/output functions and <string.h>
for string manipulation functions. We also define a constant MAX_ROMAN_NUMERAL_LENGTH
to set a limit on the length of the Roman numeral strings.
Function Prototypes
The function prototypes for intToRoman
and romanToInt
are declared to inform the compiler about these functions before their actual implementations.
Main Conversion Functions
intToRoman(int num, char *roman)
: This function takes an integer num
and converts it to a Roman numeral string stored in roman
. It uses arrays of Roman numerals and their corresponding values to build the Roman numeral string.
romanToInt(const char *roman)
: This function takes a Roman numeral string roman
and converts it to an integer. It uses arrays of Roman numerals and their corresponding values to calculate the integer value by matching substrings of the input Roman numeral string.
Main Function
The main
function demonstrates the usage of the conversion functions. It converts an integer to a Roman numeral and then converts the Roman numeral back to an integer to verify the correctness of the conversions.