Thursday, October 29, 2009

Converting to roman equivalent

Write a general-purpose function to convert any given year into its roman equivalent. The following table shows the roman equivalents of decimal numbers: Example: Roman equivalent of 1988 is mdcccclxxxviii Roman equivalent of 1525 is mdxxv



void main()
{
int a;
printf("\nInput the year to get it in Roman numerals");
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("x");
roman(year-10);
}

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

else if(year>=1)
{
printf("i");
roman(year-1);
}


}

13 comments:

  1. i am using dev c++ and i am having problem in getting results wid most of the programms.
    the prob is as in this case i m getting the "input the year......" on my dos screen....but wen i input an year and press enter , the screen goes away....my codes are here...


    #include
    #include
    #include
    #include
    int roman(int);
    int main()
    {
    int a;
    printf("\nInput the year to get it in Roman numerals");
    scanf("%d",&a);
    roman(a);
    }

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

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

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

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

    else if(year>=10)
    {
    printf("x");
    int roman(year-10);
    }

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

    else if(year>=1)
    {
    printf("i");
    int roman(year-1);
    }


    }

    ReplyDelete
  2. include header file
    #include
    and before ending the code or before return 0; statement getch();
    and run it works......1000%

    ReplyDelete
  3. #include
    #include

    main(){

    int year;
    printf ( "\nEnter any year" ) ;
    scanf ( "%d", &year) ;
    int romanNumber = convertYears(year) ;
    //printf ( "Factorial value = %d",romanNumber) ;





    }
    convertYears(int year){
    int numArray[]={1,5,10,50,100,500,1000};
    int symArray[]={'i','v','x','l','c','d','m'};
    int i=6;
    int tempYear=year;
    for(i=5;i>=0;i--){

    while(tempYear/numArray[i]>0){
    int countDivide=tempYear/numArray[i];

    while(countDivide!=0){
    printf("Anser is =%c \n",symArray[i]);
    countDivide--;
    }


    tempYear=tempYear%numArray[i];
    if(tempYear<numArray[i]){
    break;
    }
    //printf("num==%d",numArray[i]);
    //printf("temp year==%d",tempYear);

    ReplyDelete
  4. is not roman numeral for 1988 mcmlxxxviii instead of mdcccclxxxviii?

    ReplyDelete
  5. Hi ,
    below code is not working and i am getting only first number, i mean i gave 1991 but i am getting "m' only so can any let me know why please...


    #include
    void main()
    {
    int a;
    printf("\nInput the year to get it in Roman numerals");
    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("x");
    roman(*year-10);
    }

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

    else if(*year>=1)
    {
    printf("i");
    roman(*year-1);
    }


    }

    ReplyDelete
  6. /*
    * Year_to_Roman_Year.c
    *
    * Created on: Jul 11, 2015
    * Author: Sujay
    */

    #include
    #include

    void main()
    {
    int yr;

    printf("\nEnter the year: ");
    fflush(stdout);
    scanf("%d",&yr);
    roman_yr(yr);

    }

    void roman_yr(yr)
    {
    int i,j,k,l,sum=1,roman;
    i=yr/1000;
    j=yr%1000;

    while(sum<=i)
    {
    printf("m");
    fflush(stdout);
    sum++;
    }

    sum=1;
    k=j/500;
    l=j%500;

    while(sum<=k)
    {
    printf("d");
    fflush(stdout);
    sum++;
    }

    sum=1;
    i=l/100;
    j=l%100;


    while(sum<=i)
    {
    printf("c");
    fflush(stdout);
    sum++;
    }


    sum=1;
    k=j/50;
    l=j%50;

    while(sum<=k)
    {
    printf("l");
    fflush(stdout);
    sum++;
    }

    sum=1;
    i=l/10;
    j=l%10;

    while(sum<=i)
    {
    printf("x");
    fflush(stdout);
    sum++;
    }

    sum=1;
    k=j/5;
    l=j%5;

    while(sum<=k)
    {
    printf("v");
    fflush(stdout);
    sum++;
    }

    sum=1;

    while(sum<=l)
    {
    printf("i");
    fflush(stdout);
    sum++;
    }

    }

    ReplyDelete
  7. #include
    #include
    main()
    {
    /* roman decimal equivalent

    1 i
    5 v
    10 x
    50 l
    100 c
    500 d
    1000 m
    */
    int n;
    printf("program to convert any year into roman equivalent\nenter the year\t");
    scanf("%d",&n);
    printf("roman equivalent is ");
    fun(n);


    }
    fun(int n)
    {
    while((n-1000)>=0)
    {
    printf("m");
    n=n-1000;}
    while((n-500)>=0)
    {
    printf("d");
    n=n-500;
    }
    while((n-100)>=0)
    {
    printf("c");
    n=n-100;
    }
    while((n-50)>=0)
    {
    printf("l");
    n=n-50;
    }
    while((n-10)>=0)
    {
    printf("x");
    n=n-10;
    }while((n-5)>=0)
    {
    printf("v");
    n=n-5;
    }
    while((n-1)>=0)
    {
    printf("i");
    n=n-1;
    }
    }

    ReplyDelete
  8. #include
    int Roman();
    int main(){
    int n;
    printf("Enter any number whose roman form you want");
    scanf("%d",&n);
    Roman(n);
    return 0 ;
    }
    int Roman(int x){

    do{
    if(x>=1000)
    {
    printf("M");
    x=x-1000;
    }
    }while(x>1000);
    if(x>=500)
    {
    printf("D");
    x=x-500;
    }
    do{
    if(x>=100)
    {
    printf("C");
    x=x-100;
    }
    }while(x>100);
    if(x>=50)
    {
    printf("L");
    x=x-50;
    }

    do{
    if(x>=10)
    {
    printf("X");
    x=x-10;
    }
    }while(x>10);
    if(x>=5)
    {
    printf("V");
    x=x-5;
    }
    do{
    if(x>=1)
    {
    printf("I");
    x=x-1;
    }
    }while(x>=1);

    }

    ReplyDelete
  9. I am reading let us c and i think that the codes written above have things that aren't mentioned in the book , which book i should read along with let us c .

    ReplyDelete
  10. #include
    int main()
    { int y;
    void fx(int);
    scanf("%d",&y);
    fx(y);
    }
    void fx(int y)
    {
    int m,n,o,p,q,r,s,t,u,v,w,x;
    m=y/1000;
    n=y%1000;
    o=n/500;
    p=n%500;
    q=p/100;
    r=p%100;
    s=r/50;
    t=r%50;
    u=t/10;
    v=t%10;
    w=v/5;
    x=v%5;
    for(;m>=1;m=m-1)
    printf("m");
    for(;o>=1;o=o-1)
    printf("d");
    for(;q>=1;q=q-1)
    printf("c");
    for(;s>=1;s=s-1);
    printf("l");
    for(;u>=1;u=u-1)
    printf("x");
    for(;w>=1;w=w-1)
    printf("v");
    for(;x>=1;x=x-1)
    printf("i");

    }

    ReplyDelete