Conversion program from number to words

Conversion program from number to words

Question:

Write a program to convert the number to words.

Logic:

  • Get the input number from the user.
  • Then check the number with the individual digits with ones, tens, hundreds and so on.
  • And then print corresponding words for the numbers.

Algorithm:

  1. The numbers in words are stored in the array.
  2. Then checks the number with their position.
  3. For its ones, tens, hundreds and so on.
  4. Then print corresponding words for the corresponding numbers in the array.

Program:

[code lang=”c”]
include<stdio.h>
void main()
{
int i,k=0,a,b[7],c,d,num;
char *ones[]={"one","two","three","four","five","six","seven","eight","nine"};
char *tens[]={"eleven","twelve","thirteen","fourteen","fifteen","sistenn","seventeen","eighteen","nineteen"};
char *rou[]={"ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
clrscr();
printf("enter the number within 6 digits:lessthan 32767:");
scanf("%d",&num);
while(num>0) // seperates the dits and stores into the array
{
a=num%10;
b[k]=a;
k++;
num=num/10;
}
for(i=k-1;i>=0;i–)
{
if(i==4) // prints the thousands
{
c=b[i];
d=b[i-1];
if(b[i-1]==0)
{
printf("%s thousand ",rou[c-1]);
}
else if(c>1)
{
printf("%s %s thousand ",rou[c-1],ones[d-1]);
}
else
{
printf("%s thousand ",tens[d-1]);
}
}
if(i==3&&b[i]!=0&&k==4) //also prints the thousands <10000
{
c=b[i];
printf("%s thousand ",ones[c-1]);
}
if(i==2&&b[i]!=0) //prints the hundreds
{
c=b[i];
printf("%s hundred ",ones[c-1]);
}
if(i==1&&b[i]!=0) // prints the tens
{
c=b[i];
d=b[i-1];
if(b[i-1]==0)
{
printf("%s ",rou[c-1]);
}
else if(c>1)
{
printf("%s %s ",rou[c-1],ones[d-1]);
}
else
{
printf("%s ",tens[d-1]);
}
}
if(i==0&&b[i]!=0&&k==1) //prints the ones
{
c=b[i];
printf("%s",ones[c-1]);
}
if(b[i]==0&&k==1)
{
printf("zero");
}
}
getch();
}
[/code]

 

 

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

Your email address will not be published. Required fields are marked *