Given a number and the task is to convert the given number into words.
For Example:
Let number = 15
The outputĀ will be = “one-five”
Implementation Tips:
Make a global list with words for each digit from 0 to 9. The entries in the list will be mapped to the index, as shown in the table below.
global list = [ ‘zero’,Ā ‘one’, ……………’Nine’]
index:Ā Ā Ā Ā Ā Ā Ā Ā 0Ā Ā Ā Ā Ā 1 ……………..Ā Ā Ā 9
Examples:
Example1:
Input:
Given number = 15
Output:
The conversion of given number{ 15 } into words =
one fiveExample2:
Input:
Given number = 678
Output:
The conversion of given number{ 678 } into words =
six seven eightProgram to Convert a Number to Words (digit by digit) in Python
Method #1: Using Recursion (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Give the global list with words for each digit from 0 to 9 and store it in another variable.
- Create a recursive function say convert_numbrtoword() which accepts the given number as an argument and returns the conversion of the given number into words.
- Inside the function, check if the given number is equal to 0 using the if conditional statement.
- If it is true, then return an empty string.
- Else get the spelling for the last digit by giving the index of the global list as given number modulus 10 which gives the last digit of the given number.
- Store it in a variable.
- Add the above result and call the convert_numbrtoword() function itself by dividing the number by 10 (Recursive Logic).
- Store it in another variable.
- Return the above result.
- Pass the given number as an argument to the above convert_numbrtoword() function and print it.
- The Exit of the Program.
Below is the implementation:
# Give the global list with words for each digit from 0 to 9 and
# store it in another variable.
globl_lst = ['zero', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine']
# Create a function say convert_numbrtoword() which accepts the given number as
# an argument and returns the conversion of the given number into words.
def convert_numbrtoword(gvn_num):
# Inside the function, check if the given number is equal to 0 using the if
# conditional statement.
if(gvn_num == 0):
# If it is true, then return an empty string.
return ""
else:
# Else get the spelling for the last digit by giving the index of the global
# list as given number modulus 10 which gives the last digit of the given
# number.
# Store it in a variable.
rslt = globl_lst[gvn_num % 10]
# Add the above result and call the convert_numbrtoword() function itself by
# dividing the number by 10 (Recursive Logic).
# Store it in another variable.
fnl_rslt = convert_numbrtoword(int(gvn_num/10)) + rslt + " "
# Return the above result.
return fnl_rslt
# Give the number as static input and store it in a variable.
gvn_num = 15
print("The conversion of given number{", gvn_num, "} into words = ")
# Pass the given number as an argument to the above convert_numbrtoword()
# function and print it.
print(convert_numbrtoword(gvn_num))
Output:
The conversion of given number{ 15 } into words =
one fiveMethod #2: Using Recursion (User Input)
Approach:
- Give the number as user input using the int(input()) function.
- Store it in a variable.
- Give the global list with words for each digit from 0 to 9 and store it in another variable.
- Create a recursive function say convert_numbrtoword() which accepts the given number as an argument and returns the conversion of the given number into words.
- Inside the function, check if the given number is equal to 0 using the if conditional statement.
- If it is true, then return an empty string.
- Else get the spelling for the last digit by giving the index of the global list as given number modulus 10 which gives the last digit of the given number.
- Store it in a variable.
- Add the above result and call the convert_numbrtoword() function itself by dividing the number by 10 (Recursive Logic).
- Store it in another variable.
- Return the above result.
- Pass the given number as an argument to the above convert_numbrtoword() function and print it.
- The Exit of the Program.
Below is the implementation:
# Give the global list with words for each digit from 0 to 9 and
# store it in another variable.
globl_lst = ['zero', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine']
# Create a function say convert_numbrtoword() which accepts the given number as
# an argument and returns the conversion of the given number into words.
def convert_numbrtoword(gvn_num):
# Inside the function, check if the given number is equal to 0 using the if
# conditional statement.
if(gvn_num == 0):
# If it is true, then return an empty string.
return ""
else:
# Else get the spelling for the last digit by giving the index of the global
# list as given number modulus 10 which gives the last digit of the given
# number.
# Store it in a variable.
rslt = globl_lst[gvn_num % 10]
# Add the above result and call the convert_numbrtoword() function itself by
# dividing the number by 10 (Recursive Logic).
# Store it in another variable.
fnl_rslt = convert_numbrtoword(int(gvn_num/10)) + rslt + " "
# Return the above result.
return fnl_rslt
# Give the number as user input using the int(input()) function.
# store it in a variable.
gvn_num = int(input("Enter some random number = "))
print("The conversion of given number{", gvn_num, "} into words = ")
# Pass the given number as an argument to the above convert_numbrtoword()
# function and print it.
print(convert_numbrtoword(gvn_num))
Output:
Enter some random number = 678
The conversion of given number{ 678 } into words =
six seven eight