C Program to display the given number in words..

#include<stdio.h>
#include<string.h>

                   // numbers in words..

char ans[100]="";

char ref1[20][20]={"","one","two","three","four","five","six",
                   "seven","eight","nine","ten","eleven","twelve",
                   "thirteen","fourteen","fifteen","sixteen","seventeen",
                   "eighteen","nineteen"};
char ref2[10][10]={"","","twenty","thirty","forty","fifty","sixty",
                   "seventy","eighty","ninety"};

void stringcat1(int a,int b)
{
   if(a==1)
   {
       int temp=(a*10)+b;
       strcat(ans,ref1[temp]);
       strcat(ans," ");
   }
   else
   {
   if(a!=0)
       {
       strcat(ans,ref2[a]);
       strcat(ans," ");
       }
   if(b!=0)
       {
       strcat(ans,ref1[b]);
       strcat(ans," ");
       }
   }
}

void stringcat2(int a,int b,int c)
{
    if(a==0)
    {
        stringcat1(b,c);
    }
    else
    {
        strcat(ans,ref1[a]);
        strcat(ans," hundred and ");
        stringcat1(b,c);
    }
}
void main()
{
    long long int n,t;
    scanf("%li",&n);
    int a[10]={0};
    int i=9;
    t=n;

    // Seperating the digits..

    while(t!=0)
    {
        a[i]=t%10;
        i--;
        t/=10;
    }

    // Printing the digits in words...

    stringcat1(a[1],a[2]);
    if(a[2]!=0||a[1]!=0)
    {
    strcat(ans,"crores ");
    }
    stringcat1(a[3],a[4]);
     if(a[4]!=0||a[3]!=0)
    {
    strcat(ans,"lakhs ");
    }
    stringcat1(a[5],a[6]);
     if(a[6]!=0||a[5]!=0)
    {
    strcat(ans,"thousands ");
    }
    stringcat2(a[7],a[8],a[9]);

    printf("\nThe number in words is..\n");
    puts(ans);
}

No comments:

Post a comment