The following C Program converts a number to its correspoiding Words.
/* numtoWord.c */
/*Program to convert a no. having
maxim of 10 digitsinto equivalent words*/
//#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int ctr=0;
char str[][20] = {" ","One","Two","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten","Eleven","Twelve",
"Thirteen","fourteen","fifteen","sixteen","seventeen",
"Eighteen","Ninteen","Twenty","Thirty","Fourty","Fifty", "Sixty","Seventy","Eighty","Ninty"};
int print(int,int);
void numword(long int, int);
void main()
{
long int i=0;
int l=10,k=10;
unsigned long int no,n;
//clrscr();
printf("Enter a number( having less than 11 digit ):: ");
scanf("%ld",&n);
printf("\nThe number in words is:\n\n");
no=n;
if(no==0)
{
printf("Zero ");
printf("\n");
exit(1);
}
/*****Count no of digits*****/
for(i=n;i>0;i=i/10)
ctr++;
/*****Function call*****/
numword(no,10);
printf("\n\n");
}
/*****Recursive Function converts no: into words*****/
void numword(long int no, int k)
{
static int a,b;
static int rm1,rm2;
int rm;
rm=no%k;
if(no == 0)
return;
else
{
numword(no/k, k);
/*****Checks for the digits*****/
if(ctr==10)
{
printf("%s ",str[rm]);
printf("Billion ");
ctr=9;
}
else if(ctr==9 )
{
rm1 = rm;
ctr=8;
}
else if(ctr==8 )
{
rm1=print(rm1,rm);
if(rm1>0)
printf("Crore ");
ctr=7;
}
else if(ctr==7)
{
rm1=rm;
ctr=6;
}
else if(ctr==6 )
{
rm1=print(rm1,rm);
if(rm1>0)
printf("Lakh ");
ctr=5;
}
else if(ctr==5)
{
rm1=rm;
ctr=4;
}
else if(ctr==4)
{
rm1=print(rm1,rm);
if(rm1>0)
printf("Thousand ");
ctr=3;
}
else if(ctr==3)
{
if(rm>0)
{
printf("%s ", str[rm]);
printf("Hundred ");
}
ctr=2;
}
else if(ctr==2)
{
rm1=rm;
ctr=1;
}
else if(ctr==1)
print(rm1,rm);
}
}
/*****Function to Print equivalent words*****/
int print(int rm1,int rm)
{
int a,b;
rm1=rm1*10+rm;
/***Only one digit & hence points to the correct position***/
if(rm1>0 &&rm1<21)
printf("%s ",str[rm1]);
else
{
/*****First & 10th digit to be extracted seperately*****/
if(rm1>0)
{
a=rm1/10;
b=rm1%10;
if(a>0)
printf("%s ",str[18+a]);
if(b>0)
printf("%s ",str[b]);
}
}
/*variable rm1 to be available at the calling portion*/
return rm1;
}
Output
----------
$gcc -o numtoWord numbtoWord.c
$./numtoWord
Enter a number( having less than 11 digit ):: 1234567890
The number in words is:
One Billion Twenty Three Crore Fourty Five Lakh Sixty Seven Thousand Eight Hundred Ninty
/* numtoWord.c */
/*Program to convert a no. having
maxim of 10 digitsinto equivalent words*/
//#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int ctr=0;
char str[][20] = {" ","One","Two","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten","Eleven","Twelve",
"Thirteen","fourteen","fifteen","sixteen","seventeen",
"Eighteen","Ninteen","Twenty","Thirty","Fourty","Fifty", "Sixty","Seventy","Eighty","Ninty"};
int print(int,int);
void numword(long int, int);
void main()
{
long int i=0;
int l=10,k=10;
unsigned long int no,n;
//clrscr();
printf("Enter a number( having less than 11 digit ):: ");
scanf("%ld",&n);
printf("\nThe number in words is:\n\n");
no=n;
if(no==0)
{
printf("Zero ");
printf("\n");
exit(1);
}
/*****Count no of digits*****/
for(i=n;i>0;i=i/10)
ctr++;
/*****Function call*****/
numword(no,10);
printf("\n\n");
}
/*****Recursive Function converts no: into words*****/
void numword(long int no, int k)
{
static int a,b;
static int rm1,rm2;
int rm;
rm=no%k;
if(no == 0)
return;
else
{
numword(no/k, k);
/*****Checks for the digits*****/
if(ctr==10)
{
printf("%s ",str[rm]);
printf("Billion ");
ctr=9;
}
else if(ctr==9 )
{
rm1 = rm;
ctr=8;
}
else if(ctr==8 )
{
rm1=print(rm1,rm);
if(rm1>0)
printf("Crore ");
ctr=7;
}
else if(ctr==7)
{
rm1=rm;
ctr=6;
}
else if(ctr==6 )
{
rm1=print(rm1,rm);
if(rm1>0)
printf("Lakh ");
ctr=5;
}
else if(ctr==5)
{
rm1=rm;
ctr=4;
}
else if(ctr==4)
{
rm1=print(rm1,rm);
if(rm1>0)
printf("Thousand ");
ctr=3;
}
else if(ctr==3)
{
if(rm>0)
{
printf("%s ", str[rm]);
printf("Hundred ");
}
ctr=2;
}
else if(ctr==2)
{
rm1=rm;
ctr=1;
}
else if(ctr==1)
print(rm1,rm);
}
}
/*****Function to Print equivalent words*****/
int print(int rm1,int rm)
{
int a,b;
rm1=rm1*10+rm;
/***Only one digit & hence points to the correct position***/
if(rm1>0 &&rm1<21)
printf("%s ",str[rm1]);
else
{
/*****First & 10th digit to be extracted seperately*****/
if(rm1>0)
{
a=rm1/10;
b=rm1%10;
if(a>0)
printf("%s ",str[18+a]);
if(b>0)
printf("%s ",str[b]);
}
}
/*variable rm1 to be available at the calling portion*/
return rm1;
}
----------
$gcc -o numtoWord numbtoWord.c
$./numtoWord
Enter a number( having less than 11 digit ):: 1234567890
The number in words is:
One Billion Twenty Three Crore Fourty Five Lakh Sixty Seven Thousand Eight Hundred Ninty
No comments:
Post a Comment