You have been given an integer 'num'. Write a function to calculate descriptive word form of the number.
Use the Indian standard unit of numbering.
Example :If the given input is 62, the output should be 'Sixty two' (without quotes).
Similarly, if the input is 1000000, the output should be 'Ten lakh'.
The first line contains an integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first and only line of each test case or query contains the integer 'num' which is to be converted to words.
Output Format :
For each test case, return the number expressed in words.
Output for every test case will be printed in a separate line.
Note:- All letters except the first one should be small.
Note
You are not required to print anything, and it has already been taken care of. Just implement the function.
1 <= t <= 10^4
0 <= N <= 10^9
where 't' is the number of test cases, and 'N' is the given number.
Time Limit: 1 sec.
2
72
1
Seventy two
One
In the first test case, the given number is 72, so the output will be "Seventy two".
In the second test case, the given number is 1, so the output will be "One".
2
55
62
Fifty five
Sixty two
Every digit in the number will be converted to a different word depending on its position in that number. Hashmaps can be used to store the relation of the word, digit and its position.
The approach that can be used here is like school mathematics in which we divide the number into individual digits and handle them starting from most significant bit. So here is the representation of each digit of number:
1 2 3 4 5 6 7 8 9
| | | | | | | | |__ ones' place
| | | | | | | |__ __ tens' place
| | | | | | |__ __ __ hundreds' place
| | | | | |__ __ __ __ thousands' place
| | | | |__ __ __ __ __ ten thousand' place
| | | |__ __ __ __ __ __ lakh' place
| | |__ __ __ __ __ __ __ ten lakh' place
| |__ __ __ __ __ __ __ __ crore' place
|__ __ __ __ __ __ __ __ __ ten crore' place
So this representation is of max 9 digits and we will write code for max 9 digits.
Approach:
Take two arrays of string type, one for one’s place and another for tens place. Initialize them with strings corresponding to that place.
Now start from MSB and handle digits for ten crores' and one crore's places (if any), ten lakh's and one lakh's places (if any), ten thousands' and thousands' places (if any), hundreds' places and ones' and tens' places separately.
See the code for more detailed explanation.
O(log(n)), where n is the number you have to convert to words.
The log is in base 10. But since the maximum number of digits in the number are 9, we can say that the time complexity is constant, i.e. O(1)
O(1).
The memory allocated for the array will remain constant no matter what the number is.