Saturday, October 22, 2011

solution of balagurusamy of chapter 8.9

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

void num_to_words(long int, char[]);

char one[20][10]={" ","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE",
                    "TEN","ELEVEN","TWELVE","THIRTEEN","FORTEEN","FIFTEEN","SIXTEEN",
                    "SEVENTEEN","EIGHTEEN","NINETEEN"};
char ten[10][8]={" "," ","TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};


int main()
{
    int a,b=0,i,m;
    char n[8],*p;
    printf("Please enter the amount of money:");
    scanf("%s", n);
    a=atoi(n);
    p=strchr(n,'.');
    if(p!=NULL)
        b=atoi(p+1);
    printf("\nIn words:\n");
    for(i=1;i<=2;i++)
    {
        m=(i<2)?a:b;
        num_to_words(((m/1000)%100),"THOUSAND");
        num_to_words(((m/100)%10),"HUNDRED");
        num_to_words((m%100),"\0");
        if(i%2&&b)
            printf("AND PAISE");
    }
    return 0;
}

void num_to_words(long int n,char ch[10])
{
    if(n>19)
        printf(" %s %s",ten[n/10],one[n%10]);
    else if(n)
        printf(" %s",one[n]);
    if(n)
          printf(" %s",ch);
}

No comments:

Post a Comment