Convert A Given Number To Words

Easy
0/40
Average time to solve is 15m
profile
Contributed by
4 upvotes
Asked in companies
HSBCOracleGoldman Sachs

Problem statement

Given an integer number ‘num’. Your task is to convert ‘num’ into a word.

Suppose the given number ‘num’ is ‘9823’ then you have to return a string “nine thousand eight hundred twenty three” that is word conversion of the given ‘num’ 9823.

Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of input contains an integer ‘T’ denoting the number of test cases.

Each of the next ‘T’ lines contains a single integer ‘num’.
Output Format :
For each test case, return a string/character array that is the word form of the given number ‘num’.

Note :

1. In every two consecutive words, there must be a space.
2. All the characters of the word must be English lower letter.
3. The return type must be a string.
4. You are not required to print the output explicitly, it has already been taken care of. Just implement the function and return the word.
Constraints :
1 <= T <= 10^5
0 <= N <= 9999

Time limit: 1 sec
Sample Input 1 :
2
8743
649
Sample Output 1 :
eight thousand seven hundred forty three
six hundred forty nine
Explanation Of Sample Input 1 :
Test Case 1:


Given ‘num’ is ‘8743’ 

‘8743’ can be written as ‘ 8 x 1000 + 7 x 100 + 4 x 10 + 3’ and the word form of this number is “eight thousand seven hundred forty three”.


Test Case 2:

Given ‘num’ is ‘649’

‘649’ can be written as ‘ 6 x 100 + 4 x 10 + 9 ’ and the word form of this number is “six hundred forty nine”.
Sample Input 2 :
2
10
123
Sample Output 2 :
ten
one hundred twenty three
Hint

Try to think recursive.

Approaches (2)
Recursion

 

  • The idea is that we convert only a single first digit and call the recursive function for other remaining digits.
    • Suppose given ‘num = 1234’ then we convert only “one thousand” in an English word and call then recursion function for ‘234’.
    • Also, assume that we get “two hundred thirty four” in return.
    • So we have to return both parts “one thousand” and “two hundred thirty four” as a single part and it will be English word conversion of ‘num = 1234’
  • In this approach, we use a simple if-else condition.
  • If the length of ‘num’ is 4 then
    • Add single first digit in word and add ‘thousand’ and call the recursion for remaining ‘3’ digits.
  • If the length of ‘num’ is 3 then
    • Add single first digit in word, add ‘hundred’ and call the recursion function for remaining ‘2’ digits.
  • If the length of ‘num’ is 2 then
    • There can be two cases
      • The number is ‘10’ to ‘19’ then return the corresponding English word with help of ‘doubleDigits’ array and return the word.
      • The number is ‘20’ to ‘99’ then convert single first digit in from of ‘tensMultiple’ like ‘21’ then  ‘twenty’, ‘33’ then ‘thirty’ and call the recursion for last remaining single digit.
  • If the length of ‘num’ is 1 then
    • Return English word of single-digit.
Time Complexity

O(1), 

 

We are using at max ‘4’ step-depth recursion.

Space Complexity

O(1), 

 

We are using constant space.

Code Solution
(100% EXP penalty)
Convert A Given Number To Words
Full screen
Console