Wednesday, 16 May 2012

Write a C program that will take a number a input ( not more than 9999999) and return its value in words.

Underlying concept to demonstrate : String manipulation


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

int main()
{
    unsigned num;
    printf("\nEnter a no.(equal to less than 9999999):");
    scanf("%u",&num);
    int ar[7]={0,0,0,0,0,0,0},i=0;
    char as[10][9]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    char ap[10][9]={"one","two","three","four","five","six","seven","eight","nine"};
    char aq[10][9]={"Ten","Twenty","thirty","fourty","fifty","sixty","seventy","eignhty","ninety"};
    printf("\n\n\nNumber in words is:");
    while(num>0)
    {
        ar[i]=num%10;
        num=num/10;
        i++;
    }
    int j;
    char l[10],m[10],n[10],p[10],q[10],r[10],s[10];
    //storing the value at ten lacs place
    if(ar[6]==0)
    {
        strcpy(l,"\0");
    }
    else
    {
        if(ar[6]==1&&ar[5]==0)
        {
            strcpy(l,"Ten");
            strcpy(m,"\0");
        }
       
        else if(ar[6]==1 && ar[5]!=0)
        {
            for(j=1;j<=9;j++)
            {
                if(ar[5]==j)
                {
                    strcpy(l,"\0");
                    strcpy(m,as[j-1]);
                }
            }
        }
       
        else
        {
            for(j=2;j<=9;j++)
            {
                if(ar[6]==j)
                    strcpy(l,aq[j-1]);
            }
        }
    }

    //storing value at lacs place
    if(ar[6]!=1)
    {
        for(j=1;j<=9;j++)
        {
            if(ar[5]==j)
            strcpy(m,ap[j-1]);
        }
    }
   

    //STORING VALUE AT TEN THOUSAND PLACE
        if(ar[4]==0)
        {
                strcpy(n,"\0");
        }
        else
        {
                if(ar[4]==1&&ar[3]!=0)
                {
                        for(j=1;j<=9;j++)
                        {
                                if(ar[3]==j)
                                {
                                        strcpy(n,"\0");
                                        strcpy(p,as[j-1]);
                                }
                        }
                }
                else if(ar[4]==1&&ar[3]==0)
                {
                        strcpy(n,"Ten");
                        strcpy(p,"\0");
                }
                else
                {
                        for(j=2;j<=9;j++)
                        {
                                if(ar[4]==j)
                                        strcpy(n,aq[j-1]);
                        }
                }
        }

        //storing value at thousand place
        if(ar[4]!=1)
        {
                for(j=1;j<=9;j++)
                {
                        if(ar[3]==j)
                        strcpy(p,ap[j-1]);
                }
        }

    //storing value at hundredth place
   
    for(j=1;j<=9;j++)
    {
        if(ar[2]==j)   
            strcpy(q,ap[j-1]);
        else if(ar[2]==0)
            strcpy(q,"\0");
    }
   
    //storing the value at tenth place in words
    for(j=2;j<=9;j++)
    {
       
        if(ar[1]==j)
            strcpy(r,aq[j-1]);
        else if(ar[1]==0)
            strcpy(r,"\0");
        else if(ar[1]==1)
            for(i=1;i<=9;i++)
            {
                if(ar[0]==i)
                    strcpy(r,as[i-1]);
                strcpy(s,"\0");
            }
    }
   
    if(ar[0]==0&&ar[1]!=0)
    {
        for(j=1;j<=9;j++)
            if(ar[1]==j)
            {
                strcpy(r,aq[j-1]);
                strcpy(s,"\0");
            }
    }
   
    //storing the value at unit place in words

    for(i=1;i<=9;i++)
    {   
        if(ar[1]!=1)
        {
            if(ar[0]==i)
                strcpy(s,ap[i-1]);
            else if(ar[0]==0)
                strcpy(s,"\0");
        }
    }

    if(ar[0]==0&&ar[1]==0&&ar[2]==0&&ar[3]==0&&ar[4]==0&&ar[5]==0&&ar[6]==0)
        {
            printf("\nZero\n");
            return 0;
        }
    else if(ar[1]==0&&ar[2]==0&&ar[3]==0&&ar[4]==0&&ar[5]==0&&ar[6]==0)
        {
            printf("\n %s\n",s);
            return 0;
        }
    else if(ar[2]==0&&ar[3]==0&&ar[4]==0&&ar[5]==0&&ar[6]==0)
        {
            printf("\n %s %s\n",r,s);
            return 0;
        }
    else if(ar[3]==0&&ar[4]==0&&ar[5]==0&&ar[6]==0)
        {
            printf(" %s  hundred and %s %s",q,r,s);
            return 0;
        }
    else if(ar[4]==0&&ar[5]==0&&ar[6]==0)
        {
            printf("%s thousand %s hundred and %s %s",p,q,r,s);
            return 0;
        }
    else if(ar[5]==0&&ar[6]==0)
        {
            printf("%s %s thousand %s hundred and %s %s",n,p,q,r,s);
            return 0;
        }
    else
        {
            printf("%s  %s  lacs  %s  %s  thousand  %s  hundred  and  %s  %s",l,m,n,p,q,r,s);
            return 0;
        }

}   

0 comments

 
© 2011-2012 ProgrammingBlue
Posts RSS Comments RSS
Back to top