Convert number to words in C++ and Java

Write an efficient program to convert number to words in C++ and Java programming langauge.


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


Below diagram shows place value chart for any 12 digit positive integer –

convert number to words

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


Download   Run Code


Download   Run Code


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 above solution is constant. We can easily extend the program to handle number till 20-digits long which are less than ULLONG_MAX (Maximum value for an object of type unsigned long long int). ULLONG_MAX is equal to 18446744073709551615 in decimal assuming compiler takes 8 bytes for storage of unsigned long long int.

Author: Aditya Goel

Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂

Get great deals at Amazon

Leave a Reply

Notify of