CBSE Class 11 Computer Science Question 103 of 112

Dictionaries — Question 3

Back to all questions
3
Question

Question 3

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.)

Solution
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 
Answer