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

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of