Tutorialspoint
Problem
Solution
Submissions

Convert a Number to Words

Certification: Intermediate Level Accuracy: 30% Submissions: 10 Points: 15

Write a Python function to convert a given integer into its word representation.

Example 1
  • Input: num = 123
  • Output: "one hundred twenty-three"
  • Explanation:
    • Step 1: Break down 123 into place values: 1 hundred and 23.
    • Step 2: Convert 1 hundred to words: "one hundred".
    • Step 3: Convert 23 to words: "twenty-three".
    • Step 4: Combine the parts: "one hundred twenty-three".
    • Step 5: Return the final word representation.
Example 2
  • Input: num = 4567
  • Output: "four thousand five hundred sixty-seven"
  • Explanation:
    • Step 1: Break down 4567 into place values: 4 thousand and 567.
    • Step 2: Convert 4 thousand to words: "four thousand".
    • Step 3: Break down 567 into place values: 5 hundred and 67.
    • Step 4: Convert 5 hundred to words: "five hundred".
    • Step 5: Convert 67 to words: "sixty-seven".
    • Step 6: Combine the parts: "four thousand five hundred sixty-seven".
    • Step 7: Return the final word representation.
Constraints
  • 0 <= number <= 10^6
  • Time Complexity: O(1)
  • Space Complexity: O(1)
NumberDictionaries PwCSnowflake
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Break the number into parts (thousands, hundreds, tens, units).
  • Use dictionaries or lists to map numbers to their word equivalents.
  • Handle edge cases like zero and numbers less than 20 separately.

Steps to solve by this approach:

 Step 1: Define word lists for units, teens, and tens
 
 Step 2: Handle special case: if number is 0, return "zero"  
 Step 3: Parse thousands place (number ÷ 1000) and add appropriate words  
 Step 4: Parse hundreds place (number ÷ 100 % 10) and add appropriate words  
 Step 5: Handle tens place with special case for teens (10-19)  
 Step 6: Add units place for numbers not ending in teens  
 Step 7: Remove extra spaces and return the final word representation

Submitted Code :