Pages

Saturday, 24 June 2017

Number to word convert

Q : Write a program to convert a number into its equivalent word without using character array?

/* ##########################################
     Girfa : Student Help
     Program : Number to word converter
     for more program visit :
   ##########################################*/
#include<stdio.h>
#include<conio.h>
void single(int);
int reverce(int);
void ten(int);
void hundred(int);
void printhundred(int);
void main()
{
     int n;
     clrscr();
     printf("Enter number>> ");
     scanf("%d",&n);
     if(n>999)
          puts("Out of Range !!");
     else
     {
          if(n<10)
              single(n);
          else if(n<100)
          {
              if(n<20)
              {
                   n=reverse(n);
                   if(n==1)
                        printf(" Ten");
                   else
                        ten(n/=10);
              }
              else
              {
                   printhundred(n);
              }

          }
          else
          {
              single(n/100);
              printf(" Hundred");
              printhundred(n-(100*(reverse(n)%10)));
          }
     }
     getch();

}
void printhundred(int n)
{
     n=reverse(n);
     if(n<10)
          hundred(n);
     else
     {
          hundred(n%10);
          single(n/=10);
     }
}
void hundred(int n)
{
     char *ar[]={"","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
     printf(" %s",ar[--n]);
}
void ten(int n)
{
     char *ar[]={"","Elevan","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
     printf(" %s",ar[n]);
}
int reverse(int n)
{
     int m=0;
     while(n>0)
     {
          m=m*10+(n%10);
          n/=10;
     }
     return m;
}
void single(int n)
{
     char *ar[]={"Zero","One","Two","Three","Four","Five","Six","Seven","EIght","Nine"};
     printf(" %s",ar[n]);

}


Next Program

No comments:

Post a Comment