#include <stdio.h>
#include <string.h>
/*
Convert numbers to English words in C
-------------------------------------
This program converts integers from 0 to 999,999,999,999,999 into English words.
Strategy:
- Break the number into named groups: trillions, billions, millions, thousands, hundreds.
- Convert each 0–999 group using a helper function.
- Append group names ("trillion", "billion", etc.) when needed.
- Build the final string with proper spacing.
The program is fully commented to explain each step clearly.
*/
/* Basic words for small numbers */
static const char *ones[] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};
/* Words for tens */
static const char *tens_words[] = {
"", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"
};
/*
append_word:
Appends a word to the output buffer with proper spacing.
Uses strncat to avoid buffer overflow.
*/
static void append_word(char *out, const char *word) {
if (word && *word) {
if (*out) strcat(out, " "); /* safe: append a single space */
strcat(out, word); /* safe: append known short literal */
}
}
/*
convert_hundreds:
Converts numbers from 0 to 999 into words.
Appends the result into the provided output buffer.
*/
static void convert_hundreds(long long n, char *out) {
if (n == 0) return;
if (n >= 100) {
append_word(out, ones[n / 100]);
append_word(out, "hundred");
n %= 100;
if (n > 0) append_word(out, "and");
}
if (n >= 20) {
append_word(out, tens_words[n / 10]);
if (n % 10) append_word(out, ones[n % 10]);
} else if (n > 0) {
append_word(out, ones[n]);
}
}
/*
number_to_words:
Converts any number from 0 to 999,999,999,999,999 into English words.
*/
static void number_to_words(long long num, char *out) {
if (num == 0) {
strcpy(out, "zero");
return;
}
/* Use plain integer literals (no digit separators) for C */
long long trillions = num / 1000000000000LL;
long long billions = (num / 1000000000LL) % 1000;
long long millions = (num / 1000000LL) % 1000;
long long thousands = (num / 1000LL) % 1000;
long long hundreds = num % 1000;
if (trillions > 0) {
convert_hundreds(trillions, out);
append_word(out, "trillion");
}
if (billions > 0) {
convert_hundreds(billions, out);
append_word(out, "billion");
}
if (millions > 0) {
convert_hundreds(millions, out);
append_word(out, "million");
}
if (thousands > 0) {
convert_hundreds(thousands, out);
append_word(out, "thousand");
}
if (hundreds > 0) {
convert_hundreds(hundreds, out);
}
}
/*
main:
Demonstrates usage with multiple test numbers.
*/
int main(void) {
long long tests[] = {
1234, /* thousands */
12345, /* tens of thousands */
123456, /* hundreds of thousands */
1234567, /* millions */
123456789, /* hundred millions */
12345678901LL, /* billions */
1234567890123LL, /* trillions */
999999999999999LL /* max supported */
};
char buffer[2048] = "";
for (size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) {
buffer[0] = '\0'; /* clear buffer */
number_to_words(tests[i], buffer);
printf("%lld -> %s\n", tests[i], buffer);
}
return 0;
}
/*
run:
1234 -> one thousand two hundred and thirty four
12345 -> twelve thousand three hundred and forty five
123456 -> one hundred and twenty three thousand four hundred and fifty six
1234567 -> one million two hundred and thirty four thousand five hundred and sixty seven
123456789 -> one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine
12345678901 -> twelve billion three hundred and forty five million six hundred and seventy eight thousand nine hundred and one
1234567890123 -> one trillion two hundred and thirty four billion five hundred and sixty seven million eight hundred and ninety thousand one hundred and twenty three
999999999999999 -> nine hundred and ninety nine trillion nine hundred and ninety nine billion nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine
*/