Skip to main content

C program to print number in words

 Write a C program to input a number from user and print it into words using for loop. How to display number in words using loop in C programming. Logic to print number in words in C programming.

Example

Input

Input number: 1234

Output

One Two Three Four

Required knowledge

Basic C programmingSwitch caseWhile loop

Logic of convert number in words

Step by step descriptive logic to convert number in words.

  1. Input number from user. Store it in some variable say num.
  2. Extract last digit of given number by performing modulo division by 10. Store the result in a variable say digit = num % 10.
  3. Switch the value of digit found above. Since there are 10 possible values of digit i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 hence, write 10 cases. Print corresponding word for each case.
  4. Remove last digit from num by dividing it by 10 i.e. num = num / 10.
  5. Repeat step 2 to 4 till number becomes 0.

The above logic is correct however it print the words in reverse order. For example, 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.

Program to print number in words


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

#include <stdio.h>

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

    /* Input 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;

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

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

    /* 
     * 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

Comments

Popular posts from this blog

C program to find prime factors of a number

  Write a C program to input a number from user and find Prime factors of the given number using loop. C program to list all prime factors of a given number. Logic to find prime factors of a number in C programming. Example Input Input any number: 10 Output Prime factors of 10: 2, 5 Required knowledge Basic C programming ,  If statement ,  For loop ,  Nested loop What is Prime factor? Factors of a number  that are  prime numbers  are called as Prime factors of that number. For example: 2 and 5 are the prime factors of 10. Logic to check prime factors of a number Step by step descriptive logic to find prime factors. Input a number from user. Store it in some variable say  num . Run a loop from  2  to  num/2 , increment 1 in each iteration. The loop structure should look like  for(i=2; i<=num/2; i++) . You may think why loop from 2 to  num/2 ? Because prime number starts from 2 and any factor of a number  n  is ...

To Find Factorial Of A Number Using C Program

  Program 1: Factorial program in c using for loop #include <stdio.h> int main (){ int i , f = 1 , num ;   printf ( "Enter a number: " ); scanf ( "%d" ,& num );   for ( i = 1 ; i <= num ; i ++) f = f * i ;   printf ( "Factorial of %d is: %d" , num , f ); return 0 ; } Result Enter a number: 8 Factorial of 8 is: 40320 Program 2: Factorial program in c using pointers #include <stdio.h>   void findFactorial ( int , int *); int main (){ int i , factorial , num ;   printf ( "Enter a number: " ); scanf ( "%d" ,& num );   findFactorial ( num ,& factorial ); printf ( "Factorial of %d is: %d" , num ,* factorial );   return 0 ; }   void findFactorial ( int num , int * factorial ){ int i ;   * factorial = 1 ;   for ( i = 1 ; i <= num ; i ++) * factorial =* factorial * i ; } Result Enter a number: 8 Factorial of 8 is: 40320 Program 3: Factorial progra...

Questions of C Program

1. Write a C program to find the area of a circle. 2. Write a C program to find the area of a triangle. 3. Write a C program to swap two numbers without temp variable (or a third variable) 4. Write a C program to print ASCII value of the character 5. Write a C program to read a character in uppercase and then print in lowercase. 6. Write a C program to swap two numbers with temp variable 7. Write a C program to calculate the average of two number 8. Write a C program to calculate the simple interest. 9. Write a C program to find the quotient and remainder of two numbers 10.Write a C program that displays the size of every datatype. 1. Write a c program to add two integers and print the result ,inputs must be taken using scanf function. 2. Write a c program to add two float numbers without third variable and print the result ,inputs must be taken using scanf function. 3. Write a c program, to subtract two integers and print the result, inputs must be taken using scanf function. 4. Write...