Thursday 27 September 2012

Function006

Write a general - purpose function to convert any  given year into its roman equivalent. The following table shows the roman equivalent of decimal numbers:
Decimal                            Roman
Decimal                          Roman
1                                              i
5                                              v
10                                            x
50                                            l
100                                        c
500                                        d
1000                                      m



#include <stdio.h>
#include <conio.h>
void main ( )
{
        int          yr;
        clrscr ( );
                  
        printf("\n\n\n\n\t\t Enter year...:"); scanf("%d", &yr);
           
        printf("\n\n\t\t ROMAN EQUIVALENT...: ");
               
        yr = roman ( yr, 1000, 'm' );                              /* Function calling */
        yr = roman ( yr, 500, 'd' );
        yr = roman ( yr, 100, 'c' );
        yr = roman ( yr, 50, 'l' );
        yr = roman ( yr, 10, 'x' );
        yr = roman ( yr, 5, 'v' );
        yr = roman ( yr, 1, 'i' );
                   
        getch ( );
}                                  
                               
roman ( int    x, int   y, char    z )                                /* Function Definition */
{                
        int        a, b;
                             
        if ( x == 9 )
        {              
                  printf ( " ix " );
                  return ( x % 9 );
        }          
        if ( x == 4 )
        {              
                  printf ( " iv " );
                  return ( x % 4 );
        }                          
                               
        b = x / y;
        for ( a = 1; a <= b; a++ )
        {                               
                  printf ( "%c", z );
        }
        return ( x - y * b );
}
Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...