C Program to convert a decimal number to Roman numerals

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

Expected Output:

1st run:

2nd run:

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.

Leave a Comment

%d bloggers like this: