Last Updated: 10 Dec, 2020

Convert A Given Number To Words

Easy
Asked in companies
Goldman SachsHSBCOracle

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.

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

Approaches

01 Approach

 

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

02 Approach

The basic idea is that we convert every word according to the given number’s digit, basically, the range of number is‘0’ to ‘9999’ so we have to consider at max 4-digits numbers. Idea is that store all the parts in different arrays. One array will store single digits. One for number ‘10’ to ‘19’ another one store multiples of ‘10’.

 

  • To implement this approach we need three arrays ‘singleDigits’, ‘doubleDigits’, and ‘tensMultiple’ in which we store English words of the relative number.
  • For example, in ‘singleDigits’ we store English word of ‘0, 1, 2, 3, ……., 9’, in ‘doubleDigits’ we store English word of ‘10, 11, 12 ………., 19’ and in ‘tensMultiple’ we store English word of ‘20,30 ......., 90’.
  • Convert ‘num’ into the string.
  • Use a variable string ‘word’ to store, converted word form of ‘num’.
  • Iterate each and every digit of ‘num’.
  • If the current length of ‘num’ is three or four then convert only starting two digits of ‘num’ in an English word and store them into ‘word’ variable.
    • Then add single first digit’s of the English word in ‘word’, for example, ‘1234’ then add ‘one’ in ‘word’ with help of ‘singleDigits’ array’s elements in which we store indices wise English word.
    • If length is four then add ‘thousand’ English word in  ‘word’ else add ‘hundred’
    • For example, if the given number is ‘2345’ then we store “two thousand and three hundred” in the variable ‘word’, and in number ‘2345’ we have converted ‘23’ in English word in next step we convert ‘45’ into the English word
    • Decrease the length of ‘num’ by ‘2’ because we have already convert starting digits in English words a now only two digits remain.
  • If the current length of ‘num’ is two then convert the last two digits of ‘num’ into the English word.
    • There can we two possibilities that number can be a part of ‘10 - 19’ group or ‘20 - 99’ part group and we know both have different English word.
    • If the number is a part of ‘10-19’ group then the first digit of the number will be ‘1’
      • Then add an English word on the number with the second digit. Suppose the second digit is ‘2’ then check ‘doubleDigit[2]’ and it will be twelve
    • Else number if part of ‘20 -99’ group part.
      • Add the tenth multiple of the first single digit’s English word in ‘word’.
      • Then add the remaining single last digit in ‘word’
      • Suppose the number is ‘45’ then add ‘tensMultiple[4]’ and ‘singleDigits[5]’ in the ‘word’.
  • If the current length of ‘num’ is ‘1’ then add single digits into ‘word’ means suppose ‘3’ is a length ‘1’ number then add ‘singleDigits[3]’ in ‘word’.
  • Return ‘word’.