# Roman Numerals

Roman numerals are used frequently to indicate dates, in particular the year (for example, MMXIII represents the year 2013). There are two main differences between roman and arabic numerals.

There is no symbol for zero in roman numerals;

Numeral placement in the roman system can indicate either addition or subtraction.

Roman numeral conversion is specified using the following cases.

I represents 1. Joining two I’s together (i.e. II) represents 2, while III represents 3. However, IIII to represent 4 is forbidden because there is a shorter notation.

V represents 5. Placing I before V (i.e. IV) will mean subtract I from V, i.e. 4. Note that we do not subtract more than one I from V, so IIV to represent 3 is invalid. Placing I after V (i.e. VI) will mean add I to V, i.e. 6. Likewise, VII and VIII represents 7 and 8. To represent 9 and 10, we do not use VIIII and VV respectively because there exist shorter notations.

X represents 10. Following the rule above, IX is 9 while XI is 11.

L represents 50 and works in much the same way as V. So XL is 40 while LX is 60. Additionally, XLI is 41, XLV is 45, XLIV is 44, XLVI is 46, XLIX is 49. Hopefully you get the picture…

C represents 100.

D represents 500.

M represents 1000. M is the last character of the roman numeral system so to represent 9000, there is no choice but to use MMMMMMMMM.

Write a program that reads in a year (> 0), and outputs the year in roman numerals

Sample Runs

The following are sample runs of the program. User input is underlined. Ensure that the last line of output is followed by a newline character.

Sample run #1:

Enter the year : 1

Year 1 in roman numerals is I

Sample run #2:

Enter the year : 2013

Year 2013 in roman numerals is MMXIII

#include<stdio.h> #include<string.h> char ans[5]; void getRoman(char ch1, char ch2, char ch3, int n){ switch(n){ case 0: ans[0] = '\0'; break; case 1: ans[0]=ch1; ans[1] = '\0'; break; case 2: ans[0]=ch1; ans[1]=ch1; ans[2] = '\0'; break; case 3: ans[0]=ch1; ans[1]=ch1; ans[2]=ch1; ans[3] = '\0'; break; case 4: ans[0]=ch1; ans[1]=ch2; ans[2] = '\0'; break; case 5: ans[0]=ch2; ans[1] = '\0'; break; case 6: ans[0]=ch2; ans[1]=ch1; ans[2] = '\0'; break; case 7: ans[0]=ch2; ans[1]=ch1; ans[2]=ch1; ans[3] = '\0'; break; case 8: ans[0]=ch2; ans[1]=ch1; ans[2]=ch1; ans[3]=ch1; ans[4] = '\0'; break; case 9: ans[0]=ch1; ans[1]=ch3; ans[2] = '\0'; break; } } int main(){ char final_ans[100]; final_ans[0]='\0'; int length,i,j,temp,n,year; printf("Enter the year : "); scanf("%d",&year); n=year; if(n>=1000){ temp=n/1000; for(i=0;i<temp;i++){ final_ans[i] = 'M'; } final_ans[i] = '\0'; n%=1000; } if(n>=100){ temp = n/100; getRoman('C','D','M',temp); length = strlen(final_ans); for(i=0;i<strlen(ans);i++){ final_ans[length+i]=ans[i]; } final_ans[i+length] = '\0'; n%=100; } if(n>=10){ temp = n/10; getRoman('X','L','C',temp); length = strlen(final_ans); for(i=0;i<strlen(ans);i++){ final_ans[length+i]=ans[i]; } final_ans[i+length]='\0'; n%=10; } if(n>0){ getRoman('I','V','X',n); length = strlen(final_ans); for(i=0;i<strlen(ans);i++){ final_ans[length+i]=ans[i]; } final_ans[i+length]='\0'; } printf("Year %d in roman numerals is %s",year,final_ans); return 0; }

Explaination of logic for year 2151 -> MMCLI:

– If number(n) is greater than 1000

-Divide the number by 1000 and print that many ‘M’ . (ans= ‘MM’)

– Now calculate the new number by modulo 1000 (n = n%1000)

– If number is greater than 100 call function getRoman(‘C’,’D’,’M’,n/100);

– Append the value retured to ans . (ans = ‘MMC’)

– Now calculate the new number by modulo 100 (n = n%100)

– If number is greater than 10 call function getRoman(‘X’,’L’,’C’,n/10);

– Append the value retured to ans . (ans = ‘MMCL’)

– Now calculate the new number by modulo 10 (n = n%10)

– If number is greater than 0 call function getRoman(‘I’,’V’,’X’,n);

– Append the value retured to ans . (ans = ‘MMCLI’)

– Print final ans.