# 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 –

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:

## Java

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 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.

(1 votes, average: 5.00 out of 5)