ODD One out Quiz

Saturday, 21 June 2014

Program to Convert a number to Word In C

Program to Convert a number to word in C
Source Code in C:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
char old[70];
char ne[70];
char a[10][10]={"one ","two ","three ","four ","five ","six ","seven ","eight ","nine "};
char b[10][10]={"eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","ninteen "};
char c[10][10]={"ten ","twenty ","thirty ","fourty ","fifty ","sixty ","seventy ","eighty ","ninty "};
char d[4][10]={"hundred ","thousand ","lac ","crore "};
void func1(int);
void main()
{
 int value;
 long int r,num,x,i=1;
 unsigned long int number,n1,rev=0;
 int digit;
  while(1)
  {
  clrscr();
printf("*******To illustrate the concept of Convert a number to   word********");
  printf("\n\n1. To convert into a number into its notations like 'one thousand'");
  printf("\n\n2. To convert into a number into its in English Notaton like'One four six three'");
  printf("\n\n3. Exit option");
  printf("\n \n Enter Your Choice");
  scanf("%d",&value);
   switch(value)
   {
    case 1:
    {
    clrscr();
    strcpy(old," ");
    printf("Enter the number between 0 to 999999999 \n");
    scanf("%ld",&num);
    if(num>999999999)
    {
    exit(1);
    }
    else
    {
    x=num;
    r=x%100;
    x=x/100;
    if(r>0)
    func1(r);
    r=x%10;
    strcpy(old,ne);
    strcpy(ne,"");
     if(r>0)
       {
       strcpy(ne,a[r-1]);
       strcat(ne,d[0]);
       }
    strcat(ne,old);
    x=x/10;
    while(x>0)
    {
     strcpy(old,"");
     strcpy(old,ne);
     strcpy(ne,"");
     r=x%100;
      if(r<=0)
      i++;
      else
      {
      func1(r);
      strcat(ne,d[i++]);//hundred,lac,crore
      }
     strcat(ne,old);
     x=x/100;
    }
    printf("Your number is (in figure) =%ld \n",num);
    printf("\nResult is(In Words)= %s",strupr(ne));
    getch();
    }
          }
    break;
   case 2:
   {
    clrscr();
    printf("\n Enter a positive integer\n");
    scanf("%lu",&number);
    if(number==0)
    {
    printf("\n Zero");
    getch();
    return;
    }
    n1=number;
    while(n1!=0)
    {
    digit=n1%10;
    rev=(rev*10)+digit;
    n1=n1/10;
    }
    printf("Your number is (in figure) =%lu\n",number);
    printf("Your number is (in Word) = ");
    while(rev!=0)
    {
     digit=rev%10;
     switch(digit)
     {
     case 0:
     printf(" Zero");
     break;
     case 1:
     printf(" One");
     break;
     case 2:
     printf(" Two");
     break;
     case 3:
     printf(" Three");
     break;
     case 4:
     printf(" Four");
     break;
     case 5:
     printf(" Five");
     break;
     case 6:
     printf(" Six");
     break;
     case 7:
     printf(" Seven");
     break;
     case 8:
     printf(" Eight");
     break;
     case 9:
     printf(" Nine");
     break;
     }
     rev=rev/10;
    }
   getch();
   }
   break;
   case 3:
   exit(1);
   break;
   }
 }
getch();
}
void func1(int r)
{
 if(r<10)
 strcpy(ne,a[r-1]);        //last digit
 else if((r%10==0))
 {
 strcpy(ne,c[(r/10)-1]);
 //printf("%s \n",c[(r/10)-1]);      //fifty
 }
 else if(r>10&&r<20)
 strcpy(ne,b[r-11]);                   //fourteen
 else
 {
 strcpy(ne,c[(r/10)-1]);
 strcat(ne,a[(r%10)-1]);   //sixty nine
 }
}

This program gets a number from the user and converts it into according to user choice option. If the user chose 1 option, it converts the number in words like ‘crores thousand or lacs’. . If the user chose 2 option, it converts the number in words like that ‘one four three two nine’.