Tuesday, November 1, 2011

Convert decimail to roman

//Convert decimail nos to roman equivalent upto 10,000

/*
                     Roman      Decimal
                     1                  i
                     5                  v
                     10                x
                     50                l
                     100              c
                     500             d
                     1000           m                 */


#include<stdio.h>
#include<conio.h>
void thousand(int i) //Function for generating roman equivalent of 1000's
{
                int x;
                for(x=1;x<=i;x++)
                printf("m");
}
void hundred(int x) //Function for generating roman eq of 100's

{
                int i;
                if(x>=5)
                {
                                 x=x-5;
                                 printf("d");
                }
                for(i=1;i<=x;i++)
                printf("c");
}
void tens(int x) //Function for generating roman eq of 10's
{
                int i;
                if(x>=5)
                {
                                 x=x-5;
                                 printf("l");
                }
                for(i=1;i<=x;i++)
                printf("x");
}
void ones(int x) //Function for generating roman eq of 1's
{
                int i,j;
                if(x==9)
                                 printf("ix");
                else if(x>=5&&x<=8)
                {
                                 printf("v");
                                 for(i=1;i<=x-5;i++)
                                 printf("i");
                }
                else if(x==4)
                                 printf("iv");
                else
                {
                                 for(j=1;j<=x;j++)
                                 printf("i");
                }
}
void main() //Main program
{
                int i,y,j;
                clrscr();
        printf("Enter the year(less than 10,000):");
                scanf("%d",&y);
                printf("    ");
                for(j=1000;j>=1;j=j/10)
                {
                                 i=y/j;         //Extraction of digits for each functions
                                 if(j==1000)
                                                  thousand(i);
                                 else if(j==100)
                                                  hundred(i);
                                 else if(j==10)
                                                  tens(i);
                                 else
                                                  ones(i);
                                 y=y-(i*j); //removing the extracted digit to proceed further
                }
                getch();
}

No comments:

Post a Comment