Roman Numeral Converter in C

This program provides functions to convert numbers to Roman numerals and vice versa. It includes two main functions:

  1. intToRoman(int num, char *roman): Converts an integer to a Roman numeral string.
  2. 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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)