Sai A Sai A
Updated date Mar 13, 2024
In this blog, we will learn how to convert integers to Roman numerals using C programming.

Method 1: Direct Conversion Method

To convert an integer to a Roman numeral is by directly associating each digit with its Roman numeral counterpart. Let's take an example: suppose we have the number 1492. To convert this to a Roman numeral, we break it down into its constituent digits: 1, 4, 9, and 2. Then, we map each digit to its Roman numeral representation:

  • 1 -> I
  • 4 -> IV
  • 9 -> IX
  • 2 -> II

Concatenating these Roman numerals together, we get "MCDXCII," which is the Roman numeral representation of 1492.

Here's a C program to achieve this conversion:

#include<stdio.h>

void intToRoman(int num) {
    int decimal[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    char *roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    
    for (int i = 0; i < 13; i++) {
        while (num >= decimal[i]) {
            printf("%s", roman[i]);
            num -= decimal[i];
        }
    }
}

int main() {
    int num = 1492;
    printf("Roman numeral equivalent of %d is: ", num);
    intToRoman(num);
    return 0;
}

Output:

Roman numeral equivalent of 1492 is: MCDXCII

Method 2: Subtractive Method

In this method, we represent numbers such as 4 (IV) or 9 (IX) by subtracting a smaller numeral from a larger one. For example, 4 is represented as IV (5 - 1) and 9 is represented as IX (10 - 1).

Let's take the number 1984 as an example. We break it down into its constituent digits: 1, 9, 8, and 4. Then, we convert each digit to its Roman numeral representation:

  • 1 -> I
  • 9 -> IX
  • 8 -> VIII
  • 4 -> IV

Concatenating these Roman numerals together, we get "MCMLXXXIV," which is the Roman numeral representation of 1984.

#include<stdio.h>

void intToRoman(int num) {
    int decimal[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    char *roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    
    for (int i = 0; i < 13; i++) {
        while (num >= decimal[i]) {
            printf("%s", roman[i]);
            num -= decimal[i];
        }
    }
}

int main() {
    int num = 1984;
    printf("Roman numeral equivalent of %d is: ", num);
    intToRoman(num);
    return 0;
}

Output:

Roman numeral equivalent of 1984 is: MCMLXXXIV

Comments (0)

There are no comments. Be the first to comment!!!