# Convert Number To Words

Posted: 15 Jan, 2021

Difficulty: Easy

#### You are given an Integer ‘N’ you have to convert the integer to words.

#### For example you are given integer N = 2234 then you have to return the string “two thousand two hundred and thirty four”.

##### Input format :

```
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.
The first line of each test case contains a single integer N denoting the integer.
```

##### Output format :

```
For each test case, print a string representing the integer in words.
```

##### Note :

```
You don’t have to print anything; it has already been taken care of. Just implement the given function.
```

##### Constraints :

```
1 <= T <= 100
1 <= N <= 999999999
Time Limit: 1 sec
```

Approach 1

The idea is to iterate through the number check for the place value of each digit and starting from the extreme left, we will check by dividing the number by place value integer like crore by 10000000 and similarly by other values.

### Algorithm:

- Declare a map or an array of strings INITIALS that store numbers from one to nineteen, add an empty string also at index zero of array to compensate the indexing.
- And another array of string SECONDS that store numbers like “twenty'', “thirty'' etc till “ninety” here also add two empty strings to make the indexing simpler.
- Declare a function CONVERTDIGITSUTIL this function will take the input a number and a string STR.
- Declare an empty string RESULT.
- Inside this function check for the conditions if the number value is greater than 19 then just add string from INITIALS and SECONDS the strings which you will from both these array will at indices N%10 and N/10 respectively because ones place will have N%10th string i.e. from one to nineteen and tenth place will have numbers like twenty thirty so overall you will add RESULT += SECONDS[N/10] + INITIALS[N%10].
- Else the number will be less than nineteen so it will be present in the INITIALS array so you will add RESULT += INITIALS[N].
- Now if the number is zero then you will just return the empty string else add STR + RESULT and return it.
- Now for handling the place value of crore, lakh, thousand and hundred we will create another function HANDLEALL that will handle these values and will N as an input.
- Declare a StringBuilder SB and append string by calling function CONVERTDIGITSUTIL( N / 10000000, “crore”) and similarly for lakh, thousand and hundred.
- Now check if the number is greater 100 and N%100 != 0 and if the condition satisfies, just append “and” to the StringBuilderthan .
- Now to handle digits at ones and tens places just append string by again calling CONVERTDIGITSUTIL( N % 100, “”).
- Now return the StringBuilder SB.tostring.

SIMILAR PROBLEMS

# Palindromes And Indexes

Posted: 7 Jul, 2021

Difficulty: Moderate

# Ninja's Frustrating Homework

Posted: 8 Jul, 2021

Difficulty: Ninja

# Longest Common Prefix

Posted: 24 Jul, 2021

Difficulty: Moderate

# Hotel Rooms

Posted: 29 Jul, 2021

Difficulty: Moderate

# Matching Prefix

Posted: 21 Aug, 2021

Difficulty: Moderate