# How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two ways, either it can be printed as “one two three four five” or “twelve thousand three hundred fourty five”.

Let’s see a program to understand it clearly.

## Programs to convert a number to words:

Example 1:

``````#include<stdio.h>
#include<stdlib.h>
void numbertoword(int n)
{
switch(n)
{
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
case 0:
printf("zero ");
break;
default:
printf("tttt");
break;
}
}
int main()
{
long int n,rev=0,r;
scanf("%ld",&n);
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
n=rev;
printf("The word form is: ");
while(n>0)
{
r=n%10;
numbertoword(r);
n=n/10;
}
return 0;
}
``````

Output:

``````Please, Enter the number: 322424
The word form is: three two two four two four
``````

Explanation: In the above program, actually what we are doing is splitting each digit from the given number and representing it in its word form.

For example, consider our input that we have given now i.e., 322424. Initially, we are modulo dividing the number by 10 to get the remainder, this remainder gives us the last digit each time. We keep on doing this until the number is greater than zero. At the same time, while doing so we reverse that number. After completion, we’ll do the same process again by calling numbertoword function each time to get our result.

Now, let’s see another program that explains the second scenario i.e. representing the given number in its respective ones, tens, hundreds, thousands places etc.

Here also we follow the same approach as above by using switch statement in multiple functions. In this program, we will create separate functions for each ones digit, tens digit, hundreds digit and so on. This program can able to print the words upto billionth number as well.

Example 2:

``````#include <stdio.h>
#include<stdlib.h>
char* one(int num)
{
switch(num)
{
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
case 4: return "Four";
case 5: return "Five";
case 6: return "Six";
case 7: return "Seven";
case 8: return "Eight";
case 9: return "Nine";
default: return "";
}
}
char* ten(int num)
{
switch(num)
{
case 2: return "Twenty";
case 3: return "Thirty";
case 4: return "Forty";
case 5: return "Fifty";
case 6: return "Sixty";
case 7: return "Seventy";
case 8: return "Eighty";
case 9: return "Ninety";
default: return "";
}
}
char* lessThan20(int num)
{
switch(num)
{
case 10: return "Ten";
case 11: return "Eleven";
case 12: return "Twelve";
case 13: return "Thirteen";
case 14: return "Fourteen";
case 15: return "Fifteen";
case 16: return "Sixteen";
case 17: return "Seventeen";
case 18: return "Eighteen";
case 19: return "Nineteen";
default: return "";
}
}
char* value(long int n)
{
char* st;
snprintf(st, 10, "%d", n);
char* s = " ";
if(strlen(st)== 3)
{
long int hundred = n/100;
n = n%100;
strcat(s,one(hundred));
strcat(s," Hundred");
}
if(9 < n && n < 100)
{
if(strlen(st) != 0) strcat(s," ");
if(n < 20)
{
strcat(s,lessThan20(n));
return s;
}
else strcat(s,ten(n/10));
n = n%10;

}
if(n != 0)
{
if(strlen(st) != 0) strcat(s," ");
strcat(s, one(n));
}
return s;
}
void numberToWords(int num)
{
if(num == 0) return "Zero";
char *ans="";
long int billion = num/ (1000000000);
num %= 1000000000;
long int million = num/1000000;
num %= 1000000;
long int thousand = num/1000;
num %= 1000;
if(billion != 0)
{
strcat(ans,one(billion));
strcat(ans," Billion");
}
if(million != 0)
{
if(strlen(ans) != 0)
{
strcat(ans," ");
strcat(ans,one(million));
strcat(ans," Million");
}
}
if(thousand != 0)
{
if(strlen(ans)!= 0)
{
strcat(ans," ");
strcat(ans,one(thousand));
strcat(ans," Thousand");
}
}
if(num != 0)
{
if(strlen(ans) != 0)
{
strcat(ans," ");
strcat(ans,value(num));
}
}
printf("%s",ans);
}
int main()
{
long int n;
printf("Please, Enter the number : ");
scanf("%ld",&n);
numberToWords(n);
return 0;
}
``````

Output 1:

``````Please, Enter the number : 1234
One Thousand Thirty Four
``````

Output 2:

``````Please, Enter the number : 0
Zero
``````