Friday 26 July 2013

C program to convert numbers into billion, million, thousand (words)

/*
Name: C program to convert numbers into words 
(Short scale Arabic numbering system i.e. ten, hundred, thousand, million, billion)
Author: Prashant Jain (Google plus: https://plus.google.com/110117450187891236113)
Date: 26-07-13 14:09
Description: This program is written to demonstrate the English Translation of numbers up to 4 billion. 
Compiler: Turbo C++ 3.0
*/

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

int main()
{
 int skip1=0,skip2=0,skip3=0;
 int choice;
unsigned long n;
int p1,p2,p3,p4,p5,p6,q2,r2,s2,q3,r3,s3;
char c[20][10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen" };
char c2[11][10]={"Zero","Ten","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety","Hundred"};
char string[100]="";

while(1)
{
 p1=p2=p3=p4=p5=p6=q2=r2=s2=q3=r3=s3=0;
 printf("Enter any number upto 4 billion: ");
scanf("%d",&n);
if(n==0)
printf("zero");
p1=n/1000000000;
n=n%1000000000;

p2=n/1000000;
q2=p2/100;
p2=p2%100;
r2=p2/10;
p2=p2%10;
s2=p2;
n=n%1000000;

p3=n/1000;
 q3=p3/100;
p3=p3%100;
r3=p3/10;
p3=p3%10;
s3=p3;
n=n%1000;

p4=n/100;
n=n%100;

p5=n/10;
n=n%10;

p6=n;

if(p1!=0)
{
 strcat(string,c[p1]);
 strcat(string," Billion ");
}

if(p2!=0){
if(q2!=0){
strcat(string,c[q2]);
strcat(string," hundred ");
if(r2!=0){
if(r2==1){
strcat(string,c[10+s2]);
skip1=1;
}
if(skip1!=1){
strcat(string,c2[r2]);
}
strcat(string," ");
if(skip1!=1){
if(s2!=0){
strcat(string,c[s2]);
}

}
}
else{
strcat(string," ");
if(skip1!=1){
if(s2!=0){
strcat(string,c[s2]);
}

}
}
}
else{
if(r2!=0){
if(r2==1){
strcat(string,c[10+s2]);
skip1=1;
}
if(skip1!=1){
strcat(string,c2[r2]);
}
strcat(string," ");
if(skip1!=1){
if(s2!=0){
strcat(string,c[s2]);
}

}
}
else{
strcat(string," ");
if(s2!=0){
strcat(string,c[s2]);
}
}
}
strcat(string," million ");
}

if(p3!=0){
if(q3!=0){
strcat(string,c[q3]);
strcat(string," hundred ");
if(r3!=0){
if(r3==1){
strcat(string,c[10+s3]);
skip2=1;
}
if(skip2!=1){
strcat(string,c2[r3]);
}
strcat(string," ");
if(skip2!=1){
if(s3!=0){
strcat(string,c[s3]);
}

}
}
else{
if(s3!=0){
strcat(string,c[s3]);
}
}

}
else{
if(r3!=0){
if(r3==1){
strcat(string,c[10+p6]);
skip2=1;
}
if(skip2!=1){
strcat(string,c2[r3]);
}
strcat(string," ");
if(skip2!=1){
if(s3!=0){
strcat(string,c[s3]);
}

}
}
else{
if(s3!=0){
strcat(string,c[s3]);
}
}
}
strcat(string," thousand ");

}
if(p4!=0){
strcat(string,c[p4]);
strcat(string," hundred ");
}
if(p5!=0){
if(p5==1){
strcat(string,c[10+p6]);
skip3=1;
}
if(skip3!=1){
strcat(string,c2[p5]);
}
strcat(string," ");
}
if(skip3!=1){

if(p6!=0) {
strcat(string,c[p6]);
}
}

printf("%s",string);
choice=getch();
if(choice==27) exit(1);
printf("\n\n");
strcpy(string,"");

fflush(stdin);
}
}