Question-Write a general-purpose function to convert any given year
into its roman equivalent. The following table shows the
roman equivalents of decimal numbers:
Decimal Roman Decimal Roman
1 i 100 c
5 v 500 d
10 x 1000 m
50 l
Example:
Roman equivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv
Solution- Good question. honestly speaking, Even I used some help for this question.
#include<stdio.h>
int main()
{
int yr=1988;
printf("Enter any year\n");
scanf("%d",&yr);
yr=roman(yr,1000,109);
yr=roman(yr,500,100);
yr=roman(yr,100,99);
yr=roman(yr,50,108);
yr=roman(yr,10,120);
yr=roman(yr,5,118);
yr=roman(yr,1,105);
return 0;
}
roman(int yr, int n, int ch)
{
int a,i;
a=yr/n;
if(yr==9)
{
printf("ix");
return 0;
}
else if(yr==4)
{
printf("iv");
return 0;
}
else
{
for(i=1;i<=a;i++)
printf("%c",ch);
return(yr-a*n);
}
}
into its roman equivalent. The following table shows the
roman equivalents of decimal numbers:
Decimal Roman Decimal Roman
1 i 100 c
5 v 500 d
10 x 1000 m
50 l
Example:
Roman equivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv
Solution- Good question. honestly speaking, Even I used some help for this question.
#include<stdio.h>
int main()
{
int yr=1988;
printf("Enter any year\n");
scanf("%d",&yr);
yr=roman(yr,1000,109);
yr=roman(yr,500,100);
yr=roman(yr,100,99);
yr=roman(yr,50,108);
yr=roman(yr,10,120);
yr=roman(yr,5,118);
yr=roman(yr,1,105);
return 0;
}
roman(int yr, int n, int ch)
{
int a,i;
a=yr/n;
if(yr==9)
{
printf("ix");
return 0;
}
else if(yr==4)
{
printf("iv");
return 0;
}
else
{
for(i=1;i<=a;i++)
printf("%c",ch);
return(yr-a*n);
}
}
Why have you made cases at year = 9 and 4 in the called function?
ReplyDeleteIts bcoz if you dont define cases for 4, 9 then they will get printed as 4- IIII and not IV, for 9 - it will get printed as VIIII and not IX. And we want roman not IIII or VIIII
Deletei didn't understand why and 9? i tried my best here..
ReplyDeleteint main()
{
int year;
printf("Enter the year: ");
scanf("%d",&year);
roman(year);
}
roman(int num)
{
int i,j,k;
int temp=num;
int m,n,o,p,q,r,s;
int rem1;
if(temp>1000)
{
m=num/1000;
for(i=1;i<=m;i++)
printf("%c",'m');
temp=num-(m*1000);
}
if(temp>500)
{
n=temp/500;
for(i=1;i<=n;i++)
printf("%c",'d');
temp=temp-(n*500);
}
if(temp>100)
{
o=temp/100;
for(i=1;i<=o;i++)
{
printf("%c",'c');
}
temp=temp-(o*100);
}
if(temp>50)
{
p=temp/50;
for(i=1;i<=p;i++)
printf("%c",'l');
temp=temp-(p*50);
}
if(temp>10)
{
q=temp/10;
for(i=1;i<=q;i++)
printf("%c",'x');
temp=temp-(q*10);
}
if(temp>5)
{
r=temp/5;
for(i=1;i<=r;i++)
printf("%c",'v');
temp=temp-(r*5);
}
if(temp>1&&temp<5)
{
s=temp;
for(i=1;i<=s;i++)
printf("%c",'i');
temp=temp-s;
}
}