C program to convert a decimal number to a roman number

Here, we are going to learn how to convert a decimal number to a roman number in C programming language?
Submitted by Nidhi, on July 03, 2021

Problem Solution:

Here, we will read a decimal number and convert it into a roman number and print the result on the console screen.

Program:

The source code to convert a decimal number to a Roman Number is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to convert a decimal number to a Roman Number

#include <stdio.h>

char RomanNum[256];
int i = 0;

void previousDigit(char n1, char n2)
{
    RomanNum[i] = n1;
    i = i + 1;

    RomanNum[i] = n2;
    i = i + 1;
}

void nextDigit(char c, int n)
{
    int j = 0;
    for (; j < n; j++, i++) {
        RomanNum[i] = c;
    }
}

int main()
{
    int num = 0;
    int j = 0;

    printf("Enter the decimal number: ");
    scanf("%d", &num);

    while (num != 0) {
        if (num >= 1000) {
            nextDigit('M', num / 1000);
            num = num - (num / 1000) * 1000;
        }
        else if (num >= 500) {
            if (num < (500 + 4 * 100)) {
                nextDigit('D', num / 500);
                num = num - (num / 500) * 500;
            }
            else {
                previousDigit('C', 'M');
                num = num - (1000 - 100);
            }
        }
        else if (num >= 100) {
            if (num < (100 + 3 * 100)) {
                nextDigit('C', num / 100);
                num = num - (num / 100) * 100;
            }
            else {
                previousDigit('L', 'D');
                num = num - (500 - 100);
            }
        }
        else if (num >= 50) {
            if (num < (50 + 4 * 10)) {
                nextDigit('L', num / 50);
                num = num - (num / 50) * 50;
            }
            else {
                previousDigit('X', 'C');
                num = num - (100 - 10);
            }
        }
        else if (num >= 10) {
            if (num < (10 + 3 * 10)) {
                nextDigit('X', num / 10);
                num = num - (num / 10) * 10;
            }
            else {
                previousDigit('X', 'L');
                num = num - (50 - 10);
            }
        }
        else if (num >= 5) {
            if (num < (5 + 4 * 1)) {
                nextDigit('V', num / 5);
                num = num - (num / 5) * 5;
            }
            else {
                previousDigit('I', 'X');
                num = num - (10 - 1);
            }
        }
        else if (num >= 1) {
            if (num < 4) {
                nextDigit('I', num / 1);
                num = num - (num / 1) * 1;
            }
            else {
                previousDigit('I', 'V');
                num = num - (5 - 1);
            }
        }
    }
    printf("Roman number is: ");
    for (j = 0; j < i; j++)
        printf("%c", RomanNum[j]);

    return 0;
}

Output:

RUN 1:
Enter the decimal number: 8
Roman number is: VIII

RUN 2:
Enter the decimal number: 1010
Roman number is: MX

RUN 3:
Enter the decimal number: 88
Roman number is: LXXXVIII

Explanation:

Here, we created a global character array RomanNum and integer variable i. Then we read the decimal number and convert the number into a roman number and printed the result on the console screen.

C Number System Conversion Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.