Integer to English Words - Problem

Convert a non-negative integer num to its English words representation.

Example: Given num = 123, return "One Hundred Twenty Three"

Note: You need to handle numbers from 0 to 2,147,483,647 (2³¹ - 1)

The output should be properly capitalized with spaces between words, and no extra spaces at the beginning or end.

Input & Output

Example 1 — Basic Case
$ Input: num = 123
Output: One Hundred Twenty Three
💡 Note: 123 = 1 hundred + 20 + 3 → One Hundred Twenty Three
Example 2 — Edge Case Zero
$ Input: num = 0
Output: Zero
💡 Note: Special case: zero is handled separately
Example 3 — Large Number
$ Input: num = 1234567
Output: One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven
💡 Note: Break into groups: 1 million + 234 thousand + 567

Constraints

  • 0 ≤ num ≤ 2³¹ - 1
  • The input is guaranteed to be a valid integer

Visualization

Tap to expand
Integer to English Words Divide and Conquer with Recursion INPUT 123 num (integer) Digit Breakdown 1 Hundreds 2 Tens 3 Ones Valid Range 0 to 2,147,483,647 (0 to 2^31 - 1) Constraints Non-negative integer No leading/trailing spaces ALGORITHM STEPS 1 Define Word Maps ones[], tens[], thousands[] 2 Chunk by 1000s Split: Billion/Million/K/units 3 Process Each Chunk Recursively convert 0-999 4 Combine Results Join with proper spacing Processing 123: 123 100 + 23 1 --> "One" + "Hundred" 20 --> "Twenty" 3 --> "Three" FINAL RESULT "One Hundred Twenty Three" OK Word Mapping 1 "One Hundred" 2 "Twenty" 3 "Three" More Examples 0 --> "Zero" 12 --> "Twelve" Key Insight: Divide the number into chunks of 3 digits (thousands). Each chunk uses the same helper function to convert 0-999 to words. Then append scale words (Thousand, Million, Billion) based on position. Special cases: numbers 1-19 have unique names, and 0 returns "Zero" only if input is exactly 0. TutorialsPoint - Integer to English Words | Divide and Conquer with Recursion
Asked in
Microsoft 45 Facebook 38 Amazon 32 Google 28
78.4K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen