C program to display number in words

Write a C program to enter any number from user and print it into words using for loop. How to display number in words using loops in C programming. C program to print decimal number to words.

Example:
Input: 1234
Output: One Two Three Four

Required knowledge

Basic C programming, For loop, If else, Switch case

Logic of converting number to words

Logic to convert any number in words is pretty simple and straightforward. To convert any number into words involves basic three steps:
  1. Find the last digit of the number by performing modular division. If you don't know how check C program to find last digit of any number.
  2. Switch the appropriate word for the last digit just found.
  3. Remove last digit from the number as it isn't useful anymore.
Repeat all three steps till the number becomes 0 and finally you will get the desired output. But one problem with the above explained logic is that suppose the number is 1234 so, if you follow the above logic you will get output as "Four Three Two One" instead of "One Two Three Four". To overcome this what we need to do is reverse the number before performing above mentioned three tasks and you will get the correct output.

Program to print number in words

/**
 * C program to print number in words
 */

#include <stdio.h>

int main()
{
    int n, num = 0, i;

    /* Reads a number from user */
    printf("Enter any number to print in words: ");
    scanf("%d", &n);

    /* Finds reverse of the number */
    while(n!=0)
    {
        num = (num * 10) + (n % 10);
        n /=10;
    }

    /* 
     * Finds last digit of the number and print corresponding digit in words
     * till num becomes 0
     */
    while(num!=0)
    {
        switch(num%10)
        {
            case 0: printf("Zero ");
                break;
            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;
        }
        
        num = num/10;
    }

    return 0;
}

Update: As observed by one of our readers the above programs fails to show correct output for any integer that ends with 0. Such as 1000, 1090, 10 etc. Therefore I have made little changes in the above program to remove the bug. I have also commented the program well enough so that it could be easy to get the logic.

Program to display number in words

/**
 * C program to display number in words
 */

#include <stdio.h>
#include <math.h>

int main()
{
    int n, num = 0, i, digits;

    /* Reads a number from user */
    printf("Enter any number to print in words: ");
    scanf("%d", &n);
    
    digits = (int)log10(n); //Counts total number of digits in n

    /* Finds reverse of the number */
    while(n!=0)
    {
        num = (num * 10) + (n % 10);
        n /=10;
    }
    
    digits =  digits - ((int)log10(num));  //Counts total number of trailing zeros

    /* 
     * Finds last digit of the number and print corresponding number in words 
     * till num becomes 0
     */
    while(num!=0)
    {
        switch(num%10)
        {
            case 0: printf("Zero ");
                break;
            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;
        }
        
        num /= 10;
    }
    
    /* Prints all trailing zeros */
    while(digits)
    {
        printf("Zero ");
        digits--;
    }
    
    return 0;
}


Output
X
_
Enter any number to print in words: 1007
One Zero Zero Seven

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment