Given a positive number n, convert it into words. Assume that n <= 10000000000.

For example,

Input:  21522329901
 
Output: Twenty One Billion, Fifty Two Crore, Twenty Three Lakh, Twenty Nine Thousand Nine Hundred and One
 
 
Input:  14632
 
Output: Fourteen Thousand Six Hundred and Thirty Two

Practice this problem

The following diagram shows a place value chart for a 12-digit positive number:

 
convert numbers to words

 
We can easily solve this problem by splitting the given number into individual digits based on the above place value chart starting from the least significant digit, as shown below in C++, Java, and Python:

C++


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
Ninety Nine
One Thousand
Fourteen Thousand, Six Hundred and Thirty Two
Ninety Nine Crore, Seventy Seven Lakh, Fifty One Thousand and Seventy Six
Two Billion, Fourteen Crore, Seventy Four Lakh, Eighty Three Thousand, Six Hundred and Forty Seven

The time complexity of the above solution is constant. We can easily extend the C++ program to handle numbers till 20-digits long, less than ULLONG_MAX (The maximum value for an object of type unsigned long long int). An ULLONG_MAX is equal to 18446744073709551615 in decimal, assuming that the compiler takes 8 bytes for its storage.