Question:
Program to convert an integer number to words
example : input 4721 , output : four thousand and seven hundred twenty one
Logic:
-
We define dictionaries to map digits (0-9) to their corresponding words and place values (1, 10, 100, 1000) to their words.
-
We extract each digit from the input number and find its word representation.
-
We combine the digit word with the appropriate place word (e.g., “four” + “thousand”).
-
Finally, we join all the words to form the complete representation.
For the number 4721, the output will be “four thousand seven hundred twenty one.”
Solution in python:
At the time of interview give 2 implementation.
We can improve implementation 2 to go with 100 and more, but this is the basic idea. But I feel implementation 1 is more sufficient.
Input : 72
output : seventy two
Preparing for interview?
-
Checkout our Interview prep page, It might help you.