×

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;    
printf("Please, Enter the number: ");
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

Related Topics

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.