Conver the given year to Roman Numerals using Functions.

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
1.....................i
5....................v
10..................x
50..................l
100................c
500...............d
1000.............m

Example:
Roman equivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv

This program is a big lengthy owing to the use of Case Statements. This program can also be rewritten using Arrays, which will reduce the length considerably.


#include<stdio.h>
main()
{

int year;
int convert (int year);



{

printf("Note:Enter a four year digit year.\n\n");

printf("Enter the year that you wanna convert to Roman: " );

scanf ("%d", &year);

if (year> 1999)

{
printf("Invalid Year.Please enter again.\n\n");
}
}

convert(year);


}



convert(int year)

{
int i;

printf("\nYear converted to Roman:");


i=(year/1000); //thousands place
if(i==1)

{
printf("m");
}


i=((year/100)%10); //hundreds place

switch (i)
{
case 1:
printf("c");

break;

case 2:
printf("cc");

break;

case 3:
printf("ccc");

break;

case 4:
printf("cd");

break;

case 5:
printf("d");

break;

case 6:
printf("dc");

break;

case 7:
printf("dcc");

break;

case 8:
printf("dccc");

break;

case 9:
printf("dcccc"); //this part you may think is wrong..9 -> cm

break; //but i have taken a hint from the example in the question.

}



i=((year/10)%10); //tens place

switch(i)
{
case 1:
printf("x");

break;

case 2:
printf("xx");

break;

case 3:
printf("xxx");

break;

case 4:
printf("xl");

break;

case 5:
printf("l");

break;

case 6:
printf("lx");

break;

case 7:
printf("lxx");

break;

case 8:
printf("lxxx");

break;

case 9:
printf("lxxxx"); //had it not been for this example, it would have been xc

break;

}



i=year%10; //ones place

switch(i)
{
case 1:
printf("i");

break;

case 2:
printf("ii");

break;

case 3:
printf("iii");

break;

case 4:
printf("iv");

break;

case 5:
printf("v");

break;

case 6:
printf("vi");

break;

case 7:
printf("vii");

break;

case 8:
printf("viii");

break;

case 9:
printf("ix");

break;
}


printf ("\n\n");

return 0;

}







The file can be found at:
Download File

Comments

bbb said…
using recursion

void main()
{
int a;
printf("\nInput the year");
scanf("%d",&a);
roman(a);
}

roman(int year)
{
if(year>=1000)
{
printf("m");
roman(year-1000);
}

else if(year>=500)
{
printf("d");
roman(year-500);
}

else if(year>=100)
{
printf("c");
roman(year-100);
}

else if(year>=50)
{
printf("l");
roman(year-50);
}

else if(year>=10)
{
printf("m");
roman(year-1000);
}

else if(year>=5)
{
printf("v");
roman(year-5);
}

else if(year>=1)
{
printf("i");
roman(year-1);
}
}
-----------------------
jelly.fishinq@gmail.com
surya said…
become a follower for free C,C++ And vb code of my blog:
http://www.a2zhacks.blogspot.com to learn C,C++,VB programming.
Anonymous said…
main()
{
int year;
printf("Enter the year");
scanf("%d",&year);
roman(year);
}
roman(int a)
{
int x,p,q,r,s,t;
x=a;
p=x/1000;
x=x%1000;
q=x/100;
x=x%100;
r=x/10;
s=x%10;
for (;p>=1;p--)
printf ("\n m");
if (q>5)
{
t = q -5;
printf("d");
for (;t>=1;t--)
printf("c");
}
else if (q<5)
{
for (;q>=1;q--)
printf("c");
}
else
printf ("d");
if (r>5)
{
t = r-5;
printf("l");
for (;t>=1;t--)
printf("x");
}
else if (r<5)
{
for(;r>=1;r--)
printf("x");
}
else
printf ("l");
if (s>5)
{
t=s-5;
printf("v");
for(;t>=1;t--)
printf("i");
}
else if (s<5)
{
for(;s>=1;s--)
printf("i");
}
else
printf("v");
getch();
}

Popular posts from this blog

C Program - Calculation of Area and Circumference of a Circle using Pointers

Matchstick Game using C Programming

Generation of Fibonacci Sequence using Recursion.