C program to display number in words

Previous Program Next Program

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. Logic to print number in words in C program.

Example

Input

Input number: 1234

Output

One Two Three Four

Required knowledge

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

Logic of convert number in words

To make things simple I have divided the logic in four easy steps. Below is the step by step descriptive logic.

  1. Read a number from user. Store it in some variable say num.
  2. Extract the last digit of num by performing modulo division by 10. Store it in some variable say digit = num % 10.
  3. Remove the last digit of num by dividing it by 10. I removed last digit from num as it is not needed anymore.
  4. Switch the value of digit found just above. Print the corresponding word for each digit.
Repeat steps 2-4 till number becomes 0. Finally you are left of desired output.

The above logic works fine. But there is one problem with the above logic. Suppose number is 1234, if you apply above logic the output printed is - "Four Three Two One" instead of "One Two Three Four". To overcome this, you must reverse the number before you repeat steps 2-4.

Program to print number in words

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

#include <stdio.h>

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

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

    /* Store reverse of n in num */
    while(n != 0)
    {
        num = (num * 10) + (n % 10);
        n /= 10;
    }

    /* 
     * Extract last digit of 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, digits;

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

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

    /* 
     * Extract last digit of 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;
    }
    
    /* Print all trailing zeros */
    while(digits)
    {
        printf("Zero ");
        digits--;
    }
    
    return 0;
}
Output
Enter any number to print in words: 1007
One Zero Zero Seven

Happy coding ;)

You may also like

Previous Program Next Program
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
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment