HomeC ProgrammingC Program to Convert Numbers to Roman Numerals

C Program to Convert Numbers to Roman Numerals

This C program converts decimal numbers to Roman numerals using a function called convertToRoman. It takes user input, checks if the input is within the valid range, and then calls the conversion function to display the Roman numeral equivalent.

Problem statement

Write a C program that accepts a decimal number between 1 and 3999 from the user and converts it into Roman numerals.

C Program to Convert Numbers to Roman Numerals

// C program to convert numbers to Roman numerals

#include <stdio.h>

// Function to convert decimal to Roman numerals
void convertToRoman(int num) {
    // Arrays storing Roman numerals and their corresponding values
    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"};
  
    // Calculate the Roman numeral equivalent
    int i = 0;
    while (num > 0) {
        int quotient = num / decimal[i];
        num %= decimal[i];
        
        // Append the Roman numeral equivalent to the output
        for (int j = 0; j < quotient; j++) {
            printf("%s", roman[i]);
        }
        
        i++;
    }
}

int main() {
    int number;
    
    // Prompt the user for input
    printf("Enter a number: ");
    scanf("%d", &number);
    
    // Check if the number is within the valid range
    if (number < 1 || number > 3999) {
        printf("Invalid input. Please enter a number between 1 and 3999.\n");
        return 0;
    }
    
    // Convert the number to Roman numerals and display the result
    printf("Roman numeral equivalent: ");
    convertToRoman(number);
    printf("\n");
    
    return 0;
}

How it works?

  1. The program begins by defining a function called convertToRoman that takes an integer num as a parameter.
  2. Inside the function, two arrays are declared: decimal[] to store the decimal values of Roman numerals and roman[] to store the corresponding Roman numeral characters.
  3. The function uses a while loop to iterate through the decimal values, starting with the largest (1000) and moving to the smallest (1).
  4. In each iteration, the function calculates the quotient of num divided by the current decimal value and stores it in the variable quotient. It then calculates the remainder of the division and updates num accordingly.
  5. A nested for loop is used to print the Roman numeral character quotient number of times.
  6. After the loop finishes, the function returns to the main program.
  7. In the main function, the user is prompted to enter a number. The input is read using scanf and stored in the variable number.
  8. The program checks if the entered number is within the valid range (1 to 3999) and displays an error message if it’s not.
  9. If the number is valid, the program calls the convertToRoman function and passes the number as an argument.
  10. The Roman numeral equivalent is printed on the screen.

Input/Output

C Program to Convert Numbers to Roman Numerals

Share:

Leave A Reply

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

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...