AlgoMaster Logo

Integer to English Words

hardFrequencyUpdated September 21, 2026

Understanding the Problem

We need to convert a number into the exact English phrase used when reading it aloud. So 1234567 becomes "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven". The phrasing is easy to say but the implementation carries several edge cases.

English groups numbers into chunks of three digits, separated by scale words: Thousand, Million, Billion. Within each three-digit chunk the conversion follows one pattern: handle the hundreds digit, then the tens and ones. Two complications make this more than a lookup table. Numbers 11-19 have unique names and do not split into "tens + ones", so they need their own entries. And zeros require care: "Zero" appears only when the entire number is 0, never inside a chunk, so a chunk of "000" must contribute no words.

The constraint 0 <= num <= 2^31 - 1 means the maximum value is 2,147,483,647, which reads as "Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven". We only need to handle up to Billions.

Key Constraints:

  • 0 <= num <= 2^31 - 1 → The maximum value is about 2.1 billion, so we need exactly four scale levels: ones, thousands, millions, and billions. Nothing beyond Billion is reachable.
  • The output must have no leading or trailing spaces, and words are separated by a single space.

Approach 1: Iterative Chunk Processing

Intuition

Reading 1,234,567 aloud breaks it into groups: "One Million", "Two Hundred Thirty Four Thousand", "Five Hundred Sixty Seven". The algorithm mirrors that grouping. Process the number from right to left in chunks of three digits, convert each chunk to words, and attach the scale word for its position (Thousand, Million, Billion).

The work concentrates in one helper that converts a number from 1-999 into words. That range has three cases: the hundreds place, numbers 1-19 with their unique names, and the tens place for 20-90. With the helper in place, the main function loops through the chunks and assembles them.

Algorithm

  1. Handle the special case: if num is 0, return "Zero".
  2. Define lookup arrays for ones (1-19), tens (20, 30, ..., 90), and scale words (empty, "Thousand", "Million", "Billion").
  3. Write a helper function that converts a number from 1-999 to English words:
    • If the number is 100 or more, append the hundreds word (e.g., "Three Hundred") and continue with the remainder.
    • If the remainder is between 1 and 19, append the corresponding word directly.
    • If the remainder is 20 or more, append the tens word and then the ones word if non-zero.
  4. In the main function, process num in chunks of 1000. For each chunk:
    • Extract the last three digits using num % 1000.
    • If the chunk is non-zero, convert it to words and prepend the current scale word.
    • Divide num by 1000 and move to the next scale level.
  5. Trim and return the result.

Example Walkthrough

1Start: num=1234567. Extract first chunk: 1234567 % 1000 = 567
0
567
chunk
1
234
2
1
1/6

Code

Because the iterative approach builds chunks from right to left, it has to prepend each chunk or reverse the list at the end to restore reading order. The next approach decomposes the number top-down by scale, so the words come out in order without any reversal.

Approach 2: Recursive Decomposition

Intuition

Instead of iterating through chunks with modular arithmetic, decompose the number top-down. If the number is at least a billion, recursively convert the billions part, append "Billion", then recursively handle the remainder. The same rule applies for millions, thousands, and hundreds. Because the largest scale is emitted first, the words come out in reading order with no later reversal.

Within the base range (1-999), the same recursion applies. If the number is at least 100, convert the hundreds part and recurse on the remainder. For 20-99, take the tens word and recurse on the ones. For 1-19, look it up directly.

The recursive approach does not change the complexity (both are O(1)). It trades the explicit chunk bookkeeping for a structure that matches how the scale words nest.

Algorithm

  1. If num is 0, return "Zero".
  2. Define a recursive helper that handles any number from 1 to 2^31 - 1:
    • If num >= 1,000,000,000: recursively convert num / 1,000,000,000, append "Billion", recurse on num % 1,000,000,000.
    • Else if num >= 1,000,000: same pattern with "Million".
    • Else if num >= 1,000: same pattern with "Thousand".
    • Else if num >= 100: same pattern with "Hundred".
    • Else if num >= 20: look up the tens word, recurse on num % 10.
    • Else if num >= 1: look up the word directly.
  3. Combine all parts with spaces.

Example Walkthrough

1Start: helper(1000010). Is num >= 1,000,000,000? No. Is num >= 1,000,000? Yes.
1000010
>= 1M
1/6

Code