KnowledgeBoat Logo
LoginJOIN NOW

Computer Science

Write a program to convert a number entered by the user into its corresponding number in words. For example, if the input is 876 then the output should be 'Eight Seven Six'.
(Hint. use dictionary for keys 0-9 and their values as equivalent words.)

Python

Python Dictionaries

49 Likes

Answer

num = int(input("Enter a number: "))
d = {0 : "Zero" , 1 : "One" , 2 : "Two" , 3 : "Three" , 4 : "Four" , 5 : "Five" , 6 : "Six" , 7 : "Seven" , 8 : "Eight" , 9 : "Nine"}
digit = 0
str = ""
while num > 0:
    digit = num % 10
    num = num // 10
    str = d[digit] + " " + str

print(str)

Output

Enter a number: 589
Five Eight Nine 

Answered By

24 Likes


Related Questions