Angrezi Medium

Easy
0/40
Average time to solve is 20m
profile
Contributed by
7 upvotes
Asked in companies
AdobeMicrosoftAmazon

Problem statement

One day ninja is assigned a task by his teacher to write the numbers ‘N’ into their word form. So he started writing the number into their word form but it takes a lot of time so he comes up with the idea of writing a code which can convert the given numbers into their word form.

So help our ninja write a code that can convert a number into its word form.

So your task is to write a code that can convert numbers into their word form.

For example :
You have given a number “1234” so you have to convert it in its word form that is “0ne thousand two hundred thirty four”.
Note :
You do not need to print anything; it has already been taken care of. Just implement the function
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of input contains the ‘T’ number of test cases.

The first line of each test case contains an integer ‘N’.
Output Format :
For each test case, return its word form in the string.
Constraints :
1 <= T <= 100000
1 <= N <=  999998  

Time Limit: 1 second
Sample Input 1 :
2
9923
523
Sample Output 1 :
nine thousand nine hundred twenty three
five hundred twenty three
Explanation For Sample Input 1 :
Test Case 1 :
In the first line, there is the number of test cases i.e., 1, and in the next line‘9923’is 
The integer which we have to convert into a word form.

Here, we have started with dividing the number into individual digits and writing in its word form.
String s= ‘nine thousand’ + ‘nine hundred’ + ‘twenty three’.


Test case 2 :
String s= ‘five hundred’ + ‘twenty three’.
Sample Input 2 :
2
89
8989
Sample Output 2 :
eighty nine
eight thousand nine hundred eighty nine
Hint

Try to divide the number into individual digits

Approaches (1)
Iterative Method

We divide our numbers into individual digits and operate on them starting from the most significant digit.

 

  • We store the important words in a list like
    • One: It would store value from (“one”to “nineteen”)
    • Ten: It would store value like(“twenty”, “thirty”....up to “ninety”)
  • Now we make a function “numtowords” which takes two variable the number and the string “str1” which is used to convert the number into words when the number is ‘1’ digit or ‘2’ digit or it remains ‘1’ digit or ‘2’ digit by using this condition.
    • if (n > 19)
      • str += ten[n / 10] + one[n % 10];
    • Else
      • str += one[n];
    • And then we add “str1”at the end.
  • We call our function “numtowords”after handling “lakhs”, “thousand”and “hundred”through our function “convertToAngrezi”by using  â€˜ans= ans+  numToWords(((n / 100000) % 100), "lakh ")’;
  • Now we get our required string in ans.
Time Complexity

O(1)

 

We are performing a constant number of operations therefore the time complexity is O(1).

Space Complexity

O(1)

 

We are using a constant amount of space therefore the space complexity is O(1).

Code Solution
(100% EXP penalty)
Angrezi Medium
Full screen
Console