C program to express number into words.

There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly - The Jester
This is a program to express number into words.
Examples:
Input : 123
Output: One Hundred Twenty Three

Input 123456
Output : One Hundred Twenty Three Thousand Four Hundred Fifty Six

This post is divided into following parts.
  • Expressing two digit number in words.
  • Expressing three digit number in words.
  • Expressing number in words.
  • All code at once.

Note : This program was compiled on DEV C++

Expressing Two Digit Number In Words :-
Here is the code:

char *tens(int n){
     int a;
     char *p,temp[20];
     char t1[][10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
                  "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
     char t2[][10]={"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
     
     if(n<20){
              strcpy(temp,t1[n]);
     } else {
            a=n/10-2; 
            strcpy(temp,t2[a]);
            if((a=n%10)!=0) { 
                            strcat(temp," ");  
                            strcat(temp,t1[a]); 
            }
     }
     
     p=(char *) malloc((strlen(temp)+1)*sizeof(char));
     strcpy(p,temp);
     return(p);
}
Expressing Three Digit Number In Words:- 
It uses the tens().


char *hundred(int n){
     int a;
     char temp[40],*p;
     char t[][10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
     
     if(n>99 && n<1000){
              a=n/100;
              strcpy(temp,t[a]);
              strcat(temp," Hundred ");
              a=n%100;
              
              p=tens(a);
              strcat(temp,p);
              free(p);
     } else {
            p=tens(n);
            strcpy(temp,p);
            free(p);
     }
              p=(char *) malloc((strlen(temp)+1)*sizeof(char));
              strcpy(p,temp);
              return(p);
} 
 

Expressing Number In Words:-  

Here is the main code that uses the above functions to express number in words. 

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

#define SIZE 100
char *tens(int n);
char *hundred(int n);

int main(){
    char *p,*ptr,*temp;
    long n;
    int a,flag=0,c=0;
    char t[][10]={"Thousand","Million","Billion","Trillion"};
    
    p=(char *) malloc(SIZE * sizeof(char));                      
    temp=(char *) malloc(SIZE * sizeof(char));
    printf("\nEnter a number : ");
    scanf(" %ld",&n);
    if(n<1000){
               ptr=hundred(n);
               strcpy(p,ptr);
               free(ptr);
    } else {
           a=n%1000;
           ptr=hundred(a);
           strcpy(p,ptr);
           free(ptr);
           n=n/1000;
           while (n>0){
                a=(n>999)? n%1000 : n;
                ptr=hundred(a);
                strcpy(temp,ptr);
                strcat(temp," ");
                strcat(temp,t[c++]);
                strcat(temp," ");
                strcat(temp,p);
                free(p);
                p=(char *) malloc(SIZE * sizeof(char));                      
                strcpy(p,temp);
                free(temp);
                temp=(char *) malloc(SIZE * sizeof(char));
                free(ptr);
                n=n/1000;
           }
    }
    printf("\n %s",p);
    free(p);
    getch();  
}
All Code At Once:-
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 100
char *tens(int n);
char *hundred(int n);

int main(){
    char *p,*ptr,*temp;
    long n;
    int a,flag=0,c=0;
    char t[][10]={"Thousand","Million","Billion","Trillion"};
    
    p=(char *) malloc(SIZE * sizeof(char));                      
    temp=(char *) malloc(SIZE * sizeof(char));
    printf("\nEnter a number : ");
    scanf(" %ld",&n);
    if(n<1000){
               ptr=hundred(n);
               strcpy(p,ptr);
               free(ptr);
    } else {
           a=n%1000;
           ptr=hundred(a);
           strcpy(p,ptr);
           free(ptr);
           n=n/1000;
           while (n>0){
                a=(n>999)? n%1000 : n;
                ptr=hundred(a);
                strcpy(temp,ptr);
                strcat(temp," ");
                strcat(temp,t[c++]);
                strcat(temp," ");
                strcat(temp,p);
                free(p);
                p=(char *) malloc(SIZE * sizeof(char));                      
                strcpy(p,temp);
                free(temp);
                temp=(char *) malloc(SIZE * sizeof(char));
                free(ptr);
                n=n/1000;
           }
    }
    printf("\n %s",p);
    free(p);
    getch();  
}

char *tens(int n){
     int a;
     char *p,temp[20];
     char t1[][10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
                  "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
     char t2[][10]={"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
     
     if(n<20){
              strcpy(temp,t1[n]);
     } else {
            a=n/10-2; 
            strcpy(temp,t2[a]);
            if((a=n%10)!=0) { 
                            strcat(temp," ");  
                            strcat(temp,t1[a]); 
            }
     }
     
     p=(char *) malloc((strlen(temp)+1)*sizeof(char));
     strcpy(p,temp);
     return(p);
}

char *hundred(int n){
     int a;
     char temp[40],*p;
     char t[][10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
     
     if(n>99 && n<1000){
              a=n/100;
              strcpy(temp,t[a]);
              strcat(temp," Hundred ");
              a=n%100;
              
              p=tens(a);
              strcat(temp,p);
              free(p);
     } else {
            p=tens(n);
            strcpy(temp,p);
            free(p);
     }
              p=(char *) malloc((strlen(temp)+1)*sizeof(char));
              strcpy(p,temp);
              return(p);
}

Source Code:

Comments

Popular posts from this blog

Solution of Codeforces Round #244 (Div. 2) - Police Recruits in C.

Solution of Codechef - May Challenge 2014 - Chef Jumping in C