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:
- Count digits — find how many digits the number has and build the leading divider (e.g., 1000 for a 4-digit number).
- Walk positions left to right — extract each digit using
num / div, thennum % divto discard it. Aswitchon 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 / divgives the leftmost remaining digit.num = num % divdrops it.div /= 10shifts 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=1suppresses 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 == 0check at the top handles the special case before the main loop, which would otherwise print nothing.
Related Programs
- Recursion in C – Complete Guide
- Binary Search in C
- Complete List of C Programs
- Best Online C Compilers – Run Without Installing
Recommended Books
- The C Programming Language – Kernighan & Ritchie (India) | Amazon.com
- C Programming: A Modern Approach – K.N. King (India) | Amazon.com
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)”
Pls explain use of flag variable
Hello Deepak,
I have updated the post with explanation of the flag. Please go through it. Feel free to contact if it’s not clear.
Happy coding!
please sir write a c program to convert words to numbers
Hello Venkat,
I will write and upload it in few days.
Stay tuned.
Multiples of ten thousand e.g 10000, 20000, 30000, 40000,etc is not executable correctly