Number to words | Program to convert a given number to words

Number to words | Program to convert a given number to words

In many applications, converting a given number into its word equivalent can be quite useful. Whether it’s for a financial report, a software interface, or even for educational purposes, being able to convert a numeric value into readable words is a common task.For example, converting the number 1234 should output “one thousand two hundred thirty-four”.In this article, we will discuss how to write an algorithm that can convert any given integer into words.convert a given number to words

Problem Overview

Given a number like 9923, the task is to convert it to its word equivalent, which would be “nine thousand nine hundred twenty-three”.

Key Considerations

  • We need to handle various units of numbers, such as “thousand”, “hundred”, “ten”, and the digits themselves.
  • Special rules apply when the number contains “teen” values (like 13, 14, etc.) or compound numbers like “twenty-three”.
  • The output must be in a human-readable format with appropriate spacing and hyphenation for compound words.

Approach to Solve the Problem

To solve this problem, we’ll break the number into segments (thousands, hundreds, tens, and units) and use predefined mappings for each segment. We will convert each part into words and then combine them to form the final output.

Algorithm Steps:

  1. Define a mapping for digits to words (0-9, 10-19, tens, and thousands).
  2. Divide the number into parts: Process the number in groups (thousands, hundreds, tens, and units).
  3. Handle special cases for numbers in the “teen” range.
  4. Combine the parts and return the final word string.

Code Implementation

Here’s how we can implement this in Python:
python
def number_to_words(num): # Define word mappings for digits, tens, and thousands ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] thousands = ["", "thousand", "million", "billion"]# Base case for zero if num == 0: return “zero”def helper(num): “””Helper function to convert a number less than 1000″”” if num == 0: return “” elif num < 20: return ones[num] elif num < 100: return tens[num // 10] + ( if num % 10 == 0 else ‘-‘ + ones[num % 10]) else: return ones[num // 100] + ” hundred” + ( if num % 100 == 0 else ‘ ‘ + helper(num % 100))result = “” i = 0 while num > 0: if num % 1000 != 0: result = helper(num % 1000) + ( if thousands[i] == “” else ‘ ‘ + thousands[i]) + (‘ ‘ + result if result else ) num //= 1000 i += 1return result.strip()# Example usage number = 9923 print(number_to_words(number)) # Output: “nine thousand nine hundred twenty-three”

Explanation of the Code

  1. Mappings: We define three arrays:
    • ones: This contains the words for numbers 0-19.
    • tens: This contains the words for multiples of ten, like twenty, thirty, etc.
    • thousands: This is used for large numbers (thousand, million, etc.).
  2. Helper Function: The helper function handles numbers less than 1000. It processes numbers by breaking them into hundreds, tens, and ones:
    • For numbers less than 20, it uses the ones array.
    • For numbers between 20 and 99, it combines the appropriate multiple of ten with the ones digit (if necessary).
    • For numbers greater than 100, it handles the hundreds place and recursively processes the remaining part.
  3. Main Loop: The main loop processes the number in chunks of 1000 (thousands, millions, etc.). For each chunk, it calls the helper function and appends the corresponding thousand, million, etc., label.
  4. Final Output: The strip() method ensures that extra spaces at the ends are removed before returning the final string.

Time Complexity

The time complexity of this solution is O(n)O(n), where nn is the number of digits in the given number. Each chunk of the number is processed in constant time, and we process a maximum of three digits at a time.

Example Walkthrough

Example 1:

Input: 9923
  1. First chunk (last three digits): 923
    • 923 = “nine hundred” + “twenty-three”.
    • Result: “nine hundred twenty-three”.
  2. Second chunk: 0 (since the number is less than 1000, this part is ignored).
Output: "nine thousand nine hundred twenty-three"

Example 2:

Input: 1234
  1. First chunk: 234
    • 234 = “two hundred” + “thirty-four”.
    • Result: “two hundred thirty-four”.
  2. Second chunk: 1
    • 1 = “one thousand”.
Output: "one thousand two hundred thirty-four"

Conclusion

This solution efficiently converts a number into words using a combination of recursive functions and predefined mappings. By breaking the number into manageable chunks, we can easily handle any number, regardless of its size. convert a given number to words
c