C Program to Convert Number to Words (0 to 99999)

Converting a number to words means printing its English name: 4562 becomes four thousand five hundred sixty two. This is useful in invoice generation, banking applications, and cheque printing. The logic handles a key edge case — the teens (11–19) — where the tens digit 1 does not produce “ten X” but a single combined word like “fifteen”.

This program handles numbers from 0 to 99999 using only position arithmetic — no arrays of string literals.

How It Works

The approach has two steps:

  1. Count digits — find how many digits the number has and build the leading divider (e.g., 1000 for a 4-digit number).
  2. Walk positions left to right — extract each digit using num / div, then num % div to discard it. A switch on the current position (thousands, hundreds, tens, units) picks the right word.

The teen handling: when the tens digit is 1, set a flag instead of printing “ten”. On the next digit (units), check the flag and print the correct teen word (“fifteen”, “sixteen”, etc.) instead of the individual digit.

C Program — Number to Words (0 to 99999)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    long num, div, n1;
    int flag = 0, digit, pos, tot_dig;

    printf("Enter a number (0 - 99999): ");
    scanf("%ld", &num);

    if (num == 0) {
        printf("Zero\n");
        return 0;
    }
    if (num > 99999) {
        printf("Please enter a number between 0 and 99999.\n");
        return 1;
    }

    /* Count digits and set the leading divider */
    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 = (int)(num / div);
        num = num % div;
        div = div / 10;

        switch (pos) {
        case 2:                       /* tens */
        case 5:                       /* ten-thousands */
            if (digit == 1)
                flag = 1;             /* teen — decide on next digit */
            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("ninety ");  break;
                default: break;
                }
            }
            break;

        case 1:                       /* units */
        case 4:                       /* thousands */
            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("nineteen ");  break;
                default: break;
                }
            } 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 ");  break;
                default: break;
                }
            }
            if (pos == 4)
                printf("thousand ");
            break;

        case 3:                       /* hundreds */
            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 ");  break;
                default: break;
                }
                printf("hundred ");
            }
            break;

        default:
            break;
        }
        pos--;
    }

    printf("\n");
    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra num2words.c -o num2words
./num2words

Sample Output

Enter a number (0 - 99999): 4562
four thousand five hundred sixty two

Enter a number (0 - 99999): 15
fifteen

Enter a number (0 - 99999): 99999
ninety nine thousand nine hundred ninety nine

Enter a number (0 - 99999): 100
one hundred

Code Walkthrough

  • Digit extraction: digit = num / div gives the leftmost remaining digit. num = num % div drops it. div /= 10 shifts to the next position. For 4562 with div=1000: first pass gives digit=4, remainder=562.
  • Position counter pos: Starts at the number of digits (e.g., 4 for a 4-digit number) and decrements each pass. switch(pos) determines the place value.
  • Teen flag: When tens digit is 1, flag=1 suppresses the “ten” print. The units handler then checks the flag and prints the full teen word. This handles 10–19 correctly in both the units and ten-thousands positions.
  • Zero guard: The num == 0 check at the top handles the special case before the main loop, which would otherwise print nothing.

Related Programs

Recommended Books

Practice C with the C Programming Quiz App — 500+ MCQs covering loops, switch statements, and more.
Download on Google Play →

5 comments on “C Program to Convert Number to Words (0 to 99999)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>