×

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

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.