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.
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.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.
num is 0, return "Zero".num in chunks of 1000. For each chunk:num % 1000.num by 1000 and move to the next scale level.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.
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.
num is 0, return "Zero".num >= 1,000,000,000: recursively convert num / 1,000,000,000, append "Billion", recurse on num % 1,000,000,000.num >= 1,000,000: same pattern with "Million".num >= 1,000: same pattern with "Thousand".num >= 100: same pattern with "Hundred".num >= 20: look up the tens word, recurse on num % 10.num >= 1: look up the word directly.