Angrezi Medium

Posted: 24 Feb, 2021
Difficulty: Easy

PROBLEM STATEMENT

Try Problem

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
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
Approach 1

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.
Try Problem