Program to Convert Number to Words in C++

Write an efficient program to convert number to words.

 


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:

C++

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
Twenty One Billion, Fifty Two Crore, Twenty Three Lakh, Twenty Nine Thousand Nine Hundred and One

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 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz