write a general purpose function to convert any given year into its roman equivalent . the following table shows the roman equivalent of decimals number

decimal roman
1 i
5 v
10 x
50 l
100 c
500 d
1000 m

eg:- roman equivalen of 1988 = mdcccclxxxviii
---||-- ----||----- of 1525 = mdxxv

Recommended Answers

All 3 Replies

what will be the logic of this program be
how would be easy to break it down ?

> eg:- roman equivalen of 1988 = mdcccclxxxviii
Actually, it isn't.

It's MCMLXXXVIII

The first step is to realise that all 4 and 9 digits are written as say 4=IV or 90=XC

You then basically do /10 and %10 to extract each decimal digit, then work out what short sequence of Roman numerals represents that digit, in the place it was extracted from.

So 1988 is basically
1000 = "M"
900 = "CM", ie, 1000 - 100 - see the 4 and 9 rule above
and so on

#include<stdio.h>
2. main()
3. {
4. int year;
5. int convert (int year);
6.
7. {
8.
9. printf("Note:Enter a four year digit year.\n\n");
10. printf("Enter the year that you wanna convert to Roman: " );
11. scanf ("%d", &year);
12. if (year> 1999)
13.
14. {
15. printf("Invalid Year.Please enter again.\n\n");
16. }
17. }
18.
19. convert(year);
20. }
21.
22.
23. convert(int year)
24. {
25. int i;
26. printf("\nYear converted to Roman:");
27.
28. i=(year/1000); //thousands place
29. if(i==1)
30. {
31. printf("m");
32. }
33. i=((year/100)%10); //hundreds place
34. switch (i)
35. {
36. case 1:
37. printf("c");
38.
39. break;
40.
41. case 2:
42. printf("cc");
43.
44. break;
45.
46. case 3:
47. printf("ccc");
48.
49. break;
50.
51. case 4:
52. printf("cd");
53.
54. break;
55.
56. case 5:
57. printf("d");
58.
59. break;
60.
61. case 6:
62. printf("dc");
63.
64. break;
65.
66. case 7:
67. printf("dcc");
68.
69. break;
70.
71. case 8:
72. printf("dccc");
73.
74. break;
75.
76. case 9:
77. printf("dcccc"); //this part you may think is wrong..9 -> cm
78.
79. break; //but i have taken a hint from the example in the question.
80.
81. }
82.
83.
84.
85. i=((year/10)%10); //tens place
86.
87. switch(i)
88. {
89. case 1:
90. printf("x");
91.
92. break;
93.
94. case 2:
95. printf("xx");
96.
97. break;
98.
99. case 3:
100. printf("xxx");
101.
102. break;
103.
104. case 4:
105. printf("xl");
106.
107. break;
108.
109. case 5:
110. printf("l");
111.
112. break;
113.
114. case 6:
115. printf("lx");
116.
117. break;
118.
119. case 7:
120. printf("lxx");
121.
122. break;
123.
124. case 8:
125. printf("lxxx");
126.
127. break;
128.
129. case 9:
130. printf("lxxxx"); //had it not been for this example, it would have been xc
131. break;
132. }
133.
134.
135.
136. i=year%10; //ones place
137.
138. switch(i)
139. {
140. case 1:
141. printf("i");
142. break;
143.
144. case 2:
145. printf("ii");
146. break;
147.
148. case 3:
149. printf("iii");
150. break;
151.
152. case 4:
153. printf("iv");
154. break;
155.
156. case 5:
157. printf("v");
158. break;
159.
160. case 6:
161. printf("vi");
162. break;
163.
164. case 7:
165. printf("vii");
166. break;
167.
168. case 8:
169. printf("viii");
170. break;
171.
172. case 9:
173. printf("ix");
174. break;
175. }
176. printf ("\n\n");
177. return 0;
178. }

commented: Please check where u r posting, don't dig out dead posts !!! Also know how to post code snippets.... +0
commented: You know when there's a big theater and there's one guy clapping sarcastically in it. Yeah, it's like that. -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.