C Program to convert a given number to words (upto to 12 digit number but you can extend it)

convert a given number to words like 1=one 100= hundred 1000=thousand e.t.c. up-to  Billion(10^9)
and also its very simple to extend with copy past last while loop detail in code comments







here is code :-

#include<stdio.h>
#include<string.h>
char fi[400];
void tenpow(char fi[],long int i);//this function handle with 10's power values
void tens(char fi[],long int i)//this function Handel less than 100 value
{
  char *fst[]={" zero"," one"," two"," three"," four"," five"," six"," seven"," eight"," nine" ,
              " ten"," eleven"," twelve"," thirteen"," fourteen"," fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
   char *tens[]={ " twenty"," thirty"," forty"," fifty"," sixty"," seventy"," eighty"," ninety"};

long int cont=10,t;
if(i<20)strcat(fi,fst[i]);
else{
while(!(cont/10>i/10))cont+=10;cont-=10;
i=i%cont;t=cont/10;
 strcat(fi,tens[t-2]);if(i!=0) strcat(fi,fst[i]);

}
}

void tenpow(char fi[],long int i)
{char *tenp[]={" hundred"," Thousand"," Million"," Billion"};//if you want increase for bigger number just add next bigger string 
long int t;

if(i<100)  tens(fi,i);

   while(i>99&&i<1000){
 t=i/100 ,  i=i%100;
  if(t!=0) tens(fi,t);
   strcat(fi,tenp[0]);
  if(i!=0)   tens(fi,i);
   }

 while(i>999&&i<1000000){
   t=i/1000 ,  i=i%1000;
  if(t!=0) tenpow(fi,t);
   strcat(fi,tenp[1]);
  if(i!=0)   tenpow(fi,i);
}

 while(i>=1000000&&i<1000000000){
   t=i/1000000 ,  i=i%1000000;
  if(t!=0) tenpow(fi,t);
   strcat(fi,tenp[2]);
  if(i!=0)   tenpow(fi,i);
 }

  while(i>=1000000000&&i<1000000000000){//copy this while block extension for bigger value than billion
   t=i/1000000000 ,  i=i%1000000000;
  if(t!=0) tenpow(fi,t);
   strcat(fi,tenp[3]);//increse value by one
  if(i!=0)   tenpow(fi,i);
 }

 //paste here while block you have done just  change the values  as follow like upper 
   }

int main()
{


long int i;
printf("\n This converter example is provided by\n\t\thttps://singhcoder.blogspot.com"); 
printf("\n\nEnter Number (Maximum 12 character) : ");
scanf("%ld",&i);
if(i<1000000000000){
    tenpow(fi,i);
                    printf("\nIn words : %s\n", fi); }

else printf("\nOut of range try again with small value");
scanf("%ld");
}
//this code follow International number system count units if you want Indian please comment below

//please write comment if you want some customization or found any error or any other issue  

0 Comments