OverIQ.com

C Program to convert a decimal number to Roman numerals

Last updated on September 24, 2020


The following is a C program to convert a decimal number to Roman numerals:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
/******************************************************
 Program to convert a decimal number to roman numerals
 * 
 * Enter a number: 1996
 * Roman numerals: mmxii
 * 
 ******************************************************/

#include <stdio.h> 

int main(void) 
{   
    int num, rem;

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

    printf("Roman numerals: ");        

    while(num != 0)
    {

        if (num >= 1000)       // 1000 - m
        {
           printf("m");
           num -= 1000;
        }

        else if (num >= 900)   // 900 -  cm
        {
           printf("cm");
           num -= 900;
        }        

        else if (num >= 500)   // 500 - d
        {           
           printf("d");
           num -= 500;
        }

        else if (num >= 400)   // 400 -  cd
        {
           printf("cd");
           num -= 400;
        }

        else if (num >= 100)   // 100 - c
        {
           printf("c");
           num -= 100;                       
        }

        else if (num >= 90)    // 90 - xc
        {
           printf("xc");
           num -= 90;                                              
        }

        else if (num >= 50)    // 50 - l
        {
           printf("l");
           num -= 50;                                                                     
        }

        else if (num >= 40)    // 40 - xl
        {
           printf("xl");           
           num -= 40;
        }

        else if (num >= 10)    // 10 - x
        {
           printf("x");
           num -= 10;           
        }

        else if (num >= 9)     // 9 - ix
        {
           printf("ix");
           num -= 9;                         
        }

        else if (num >= 5)     // 5 - v
        {
           printf("v");
           num -= 5;                                     
        }

        else if (num >= 4)     // 4 - iv
        {
           printf("iv");
           num -= 4;                                                            
        }

        else if (num >= 1)     // 1 - i
        {
           printf("i");
           num -= 1;                                                                                   
        }

    }

    return 0;
}

Try it now

Expected Output: 1st run:

1
2
Enter a number: 99
Roman numerals: xcix

2nd run:

1
2
Enter a number: 2020
Roman numerals: mmxx

How it works #

The following table lists some decimal numbers and their corresponding Roman numerals:

Decimal Number Roman Numeral
1 I
4 IV
5 V
9 IX
10 X
40 XL
50 L
90 XC
100 C
400 CD
500 D
900 CM
1000 M

To convert a decimal number num to roman numerals, we do the following:

  1. Find the largest decimal number r in the above table which is less than or equal to the decimal number num.
  2. Write the Roman numeral corresponding to the decimal number r.
  3. Subtract the r from num and assign it back to num i.e num = num - r.
  4. Repeat steps 1, 2 and 3, until the num is reduced to 0.

Let's take an example.

Example: Convert decimal 123 to Roman numerals

Iteration num r Roman Numeral New Value of num
After 1st iteration 123 100 c num=123-100=23
After 2nd iteration 23 10 x num=23-10=13
After 3rd iteration 13 10 x num=13-10=3
After 4th iteration 3 1 i num=3-1=2
After 5th iteration 2 1 i num=2-1=1
After 6th iteration 1 1 i num=1-1=0

Hence, 123 = cxiii.