Python program to convert a given number into words

In this article, you will learn how to convert a given number into words. There are various approaches that help to convert a given number into words.

Approach 1:

An approach can be as follows:

Output:

Enter a number: 8955
eight thousand nine hundred fifty-five

The step-by-step implementation can be described as follows:

  • We first define three lists of words: ones contains the words for numbers 0-9, tens contains the words for multiples of 10 (excluding 10), and teens contains the words for numbers 10-19.
  • After that, we initialize empty string variable words to store the final output.
  • We use a series of if statements to check if the input number num has thousands, hundreds, tens, or ones. For each magnitude, we extract the appropriate digit(s) from the number using integer division and the modulus operator, and append the corresponding word(s) to the words variable.
  • We use a strip() method to remove any trailing spaces from the words variable, and then return the resulting string.

Time Complexity

The time complexity of the convert_to_words function is O(log n), where n is the input number. It is because the function performs a constant number of operations for each digit of the input number, and the number of digits in the input is proportional to the logarithm of the input value. Specifically, the function checks for the presence of each magnitude (thousands, hundreds, etc.) in turn, and then extracts the appropriate digit(s) and looks up the corresponding word(s) from pre-defined lists. Therefore, the time taken by the function will grow slowly as the input number increases, making it an efficient algorithm for converting numbers to words.

Approach 2

There are other approaches to converting a number to words in Python. One such approach is to use a recursive function to handle the different magnitudes of the number.

Here is an example of how you could implement this approach:

Output:

One thousand two hundred thirty-four
  • We define three lists of words: ONES contain the words for numbers 0-9, TEENS contains the words for numbers 10-19, and TENS contains the words for multiples of 10 (excluding 10).
  • We define the convert_to_words function, which takes a number num as input and recursively handles the different magnitudes of the number using a series of if statements.
  • The function first checks if the number is zero or negative, and returns the appropriate word(s) if so.
  • After that, the function checks if the number is less than 10, and returns the word for that digit.
  • If the number is between 10 and 19, the function returns the word for that number using the TEENS
  • If the number is between 20 and 99, the function recursively converts the tens digit using the TENS list, and then recursively converts the ones digit if necessary.
  • If the number is between 100 and 999, the function recursively converts the hundreds digit using the ONES list, and then recursively converts the rest of the number if necessary.
  • The function handles larger numbers (up to 999,999,999) by recursively converting the thousands, millions, and billions digits in turn, and then concatenating the resulting words.
  • The function returns the final string of words.

Approach 3

Another approach to convert a number to words in Python is to use a dictionary to map the digits to their corresponding words. Here is an example of how you could implement this approach:

Explanation:

We define a dictionary digits_to_words that maps each digit from 0 to 9 to its corresponding word. We define a list of magnitudes and their corresponding names (magnitudes). The first magnitude is 1 (for the units digit), and each subsequent magnitude is a power of 10 (3, 6, 9, etc.), along with its corresponding name ("thousand", "million", "billion", etc.).

  • We initialize an empty list words to hold the words for each group of digits.
  • We loop through the magnitudes in reverse order (starting with the largest magnitude), and for each magnitude, we check if the number is greater than or equal to that magnitude. If so, we extract the corresponding group of digits from the number and convert it to words.
  • To convert a group of three digits to words, we first check if the group contains a non-zero hundreds digit, and add the word for that digit (followed by "hundred") to the list of words if so. After that, we check if the remaining two digits represent a number between 10 and 19 (inclusive), and add the word for that number to the list of words if so. Otherwise, we add the word for the tens digit (if it is greater than or equal to 2) and the digit (if it is non-zero). The words for the tens digit and the ones digit are retrieved from the digits_to_words
  • We append the name of the current magnitude (if it is not empty) to the list of words.
  • Finally, we join the list of words together with spaces, and return the resulting string.

Time Complexity

The time complexity of this algorithm is O(log(n)), where n is the input number. It is because we loop through the magnitudes in reverse order, which takes O(log(n)) iterations in the worst case (for very large numbers). Within each iteration, the operations we perform (extracting the current group of digits, converting them to words, etc.) take constant time.

Space Complexity

The space complexity of the algorithm is also O(log(n)), because we store the words for each group of digits in a list (words). The length of this list is proportional to the number of magnitudes (which is O(log(n))), and each word takes constant space. The other data structures we use (the digits_to_words dictionary and the magnitudes list) also take constant space.






Latest Courses