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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code