# C Program to Convert Numbers to Roman Numerals

«
»

This is a C program to Convert Numbers to Roman Numerals.

Problem Description

This program takes a decimal number and converts it to roman number.

Problem Solution

1. Take a decimal number as input.
2. Check if the number is greater than 1000 or 900 or 500 or 400 or 100 or 90 or 50 or 40 or 10 or 9 or 5 or 4 or 1.
3. If it is, then store its equivalent roman number in a array.
4. Repeat the step 2-3 with the left over number.

Program/Source Code

Here is source code of the C program to Convert Numbers to Roman Numerals. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C Program to Convert Numbers to Roman Numerals`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `void predigit(char num1, char num2);`
7. `void postdigit(char c, int n);`
8. ` `
9. `char romanval;`
10. `int i = 0;`
11. `int main()`
12. `{`
13. `    int j;`
14. `    long number;`
15. ` `
16. `    printf("Enter the number: ");`
17. `    scanf("%d", &number);`
18. `    if (number <= 0)`
19. `    {`
20. `        printf("Invalid number");`
21. `        return 0;`
22. `    }`
23. `    while (number != 0)`
24. `    {`
25. `        if (number >= 1000)`
26. `        {`
27. `            postdigit('M', number / 1000);`
28. `            number = number - (number / 1000) * 1000;`
29. `        }`
30. `        else if (number >= 500)`
31. `        {`
32. `            if (number < (500 + 4 * 100))`
33. `            {`
34. `                postdigit('D', number / 500);`
35. `                number = number - (number / 500) * 500;`
36. `            }`
37. `            else`
38. `            {`
39. `                predigit('C','M');`
40. `                number = number - (1000-100);`
41. `            }`
42. `        }`
43. `        else if (number >= 100)`
44. `        {`
45. `            if (number < (100 + 3 * 100)) `
46. `            {`
47. `                postdigit('C', number / 100);`
48. `                number = number - (number / 100) * 100;`
49. `            }`
50. `            else`
51. `            {`
52. `                predigit('L', 'D');`
53. `                number = number - (500 - 100);`
54. `            }`
55. `        }`
56. `        else if (number >= 50 )`
57. `        {`
58. `            if (number < (50 + 4 * 10))`
59. `            {`
60. `                postdigit('L', number / 50);`
61. `                number = number - (number / 50) * 50;`
62. `            }`
63. `            else`
64. `            {`
65. `                predigit('X','C');`
66. `                number = number - (100-10);`
67. `            }`
68. `        }`
69. `        else if (number >= 10)`
70. `        {`
71. `            if (number < (10 + 3 * 10))`
72. `            {`
73. `                postdigit('X', number / 10);`
74. `                number = number - (number / 10) * 10;`
75. `            }`
76. `            else`
77. `            {`
78. `                predigit('X','L');`
79. `                number = number - (50 - 10);`
80. `            }`
81. `        }`
82. `        else if (number >= 5)`
83. `        {`
84. `            if (number < (5 + 4 * 1))`
85. `            {`
86. `                postdigit('V', number / 5);`
87. `                number = number - (number / 5) * 5;`
88. `            }`
89. `            else`
90. `            {`
91. `                predigit('I', 'X');`
92. `                number = number - (10 - 1);`
93. `            }`
94. `        }`
95. `        else if (number >= 1)`
96. `        {`
97. `            if (number < 4)`
98. `            {`
99. `                postdigit('I', number / 1);`
100. `                number = number - (number / 1) * 1;`
101. `            }`
102. `            else`
103. `            {`
104. `                predigit('I', 'V');`
105. `                number = number - (5 - 1);`
106. `            }`
107. `        }`
108. `    }`
109. `    printf("Roman number is: ");`
110. `    for(j = 0; j < i; j++)`
111. `        printf("%c", romanval[j]);`
112. `    return 0;`
113. `}`
114. ` `
115. `void predigit(char num1, char num2)`
116. `{`
117. `    romanval[i++] = num1;`
118. `    romanval[i++] = num2;`
119. `}`
120. ` `
121. `void postdigit(char c, int n)`
122. `{`
123. `    int j;`
124. `    for (j = 0; j < n; j++)`
125. `        romanval[i++] = c;`
126. `}`
Program Explanation

1. Take a decimal number as input and store it in the variable number.
2. Check if the number is lesser than 0. If it is, then print the output as “Invalid number”.
3. Check if the number is greater than 1000 or 500 or 100 or 50 or 10 or 5.
4. If it is, then also check if the number is greater than 900 or 400 or 90 or 40 or 9 or4. If it is, then call the function predigit() and subtract the variable number by its equivalent number and override the variable number with this value.
5. Otherwise call the function postdigit() and divide the variable number by its equivalent number and get the quotient. Multiply the quotient with its equivalent number and decrement the variable number with this value.
6. In the function postdigit(), assign the equivalent roman number to the array romanval[].
7. In the function predigit(), assign the array romanval[] with the parameters of function.
8. Repeat the steps 3-5 until the variable number becomes zero.
9. Print the array romanval[] as output.

Runtime Test Cases
```Output:

Enter the number: 500
Roman number is be: D```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms 