Number to Words - Problem

Write a function that converts a number (0-9999) to its English word representation. You must use helper functions to break down the problem into smaller parts.

Requirements:

  • Handle numbers from 0 to 9999
  • Return the English words with proper capitalization (first letter uppercase)
  • Use helper functions for different number ranges (ones, tens, hundreds, thousands)
  • No leading/trailing spaces in the result

Examples:

  • 0"Zero"
  • 42"Forty two"
  • 1234"One thousand two hundred thirty four"

Input & Output

Example 1 — Basic Single Digit
$ Input: num = 0
Output: Zero
💡 Note: Special case: zero is handled separately and returns 'Zero' with capital Z
Example 2 — Two Digit Number
$ Input: num = 42
Output: Forty two
💡 Note: 42 = 40 + 2, so we combine getTens(4)→'forty' with getOnes(2)→'two' to get 'Forty two'
Example 3 — Four Digit Number
$ Input: num = 1234
Output: One thousand two hundred thirty four
💡 Note: 1234 = 1×1000 + 2×100 + 3×10 + 4×1. We convert each part: 'One thousand' + 'two hundred' + 'thirty four'

Constraints

  • 0 ≤ num ≤ 9999
  • Return string with first letter capitalized
  • No leading or trailing spaces

Visualization

Tap to expand
INPUTALGORITHMRESULT1234Number Input1Extract thousands: 12Extract hundreds: 23Extract tens: 34Extract ones: 4Use helper functions:getOnes(), getTens(), etc.One thousand twohundred thirty fourEnglish words withproper capitalizationKey Insight:Breaking numbers into place values with helper functions creates clean, modular code that's easy to maintain and extend.TutorialsPoint - Number to Words | Helper Functions Approach
Asked in
Google 15 Microsoft 12 Amazon 8
23.4K Views
Medium Frequency
~15 min Avg. Time
890 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