May 14, 2012

C Program to convert number to words.

Write a C Program to convert number to words.
Example:
Input: 4562
Output: four thousand five hundred sixty two
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



#include<stdio.h>
#include < conio.h >
void main()
{
    long num,div,n1;
    int flag,digit,pos,tot_dig;
    
    clrscr();
    printf("\n Enter number: ");
    scanf("%ld",& num);
    if(num==0)
    {
        printf("Zero\n\n");
        exit(0);
    }
    if(num>99999)
    {
        printf("please enter number between 0 and 100000\n\n");
        exit(0);
    }
    tot_dig = 0;
    div = 1;
    n1 = num;
    while ( n1 > 9 )
    {
        n1 = n1 / 10;
        div = div * 10;
        tot_dig++;
    }
    tot_dig++;
    pos = tot_dig;
    while ( num != 0 )
    {
        digit= num / div;
        num = num % div;
        div = div / 10;
        switch(pos)
        {
            case 2:
            case 5: if ( digit == 1 )
            flag = 1;
            else
            {
                flag = 0;
                switch(digit)
                {
                    case 2: printf("twenty ");break;
                    case 3: printf("thirty ");break;
                    case 4: printf("forty ");break;
                    case 5: printf("fifty ");break;
                    case 6: printf("sixty ");break;
                    case 7: printf("seventy ");break;
                    case 8: printf("eighty ");break;
                    case 9: printf("ninty ");
                }
            }
            break;
            case 1:
            case 4:    if ( flag == 1 )
            {
                flag = 0;
                switch(digit)
                {
                    case 0 : printf("ten ");break;
                    case 1 : printf("eleven ");break;
                    case 2 : printf("twelve ");break;
                    case 3 : printf("thirteen ");break;
                    case 4 : printf("fourteen ");break;
                    case 5 : printf("fifteen ");break;
                    case 6 : printf("sixteen ");break;
                    case 7 : printf("seventeen ");break;
                    case 8 : printf("eighteen ");break;
                    case 9 : printf("ninteen ");
                }
            }
            else
            {
                switch(digit)
                {
                    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 ");
                }
            }
            if ( pos == 4 )
            printf("thousand ");
            break;
            case 3:
            if ( digit> 0 )
            {
                switch(digit)
                {
                    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 ");
                }
                printf("hundred ");
            }
            break;
        }
        pos--;
    }
    if ( pos == 4 && flag == 0)
    printf("thousand");
    else
    if ( pos == 4 && flag == 1)
    printf("ten thousand");
    if ( pos == 1 && flag == 1 )
    printf("ten ");
    getch();
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link