Program to convert a given number to words | Set 2 | C ++ program to print a given number in words . The program handles till 9 digits numbers and can be easily extended to 20 digit number ; strings at index 0 is not used , it is to make array indexing simple ; strings at index 0 and 1 are not used , they is to make array indexing simple ; n is 1 - or 2 - digit number ; if n is more than 19 , divide it ; if n is non - zero ; Function to print a given number in words ; stores word representation of given number n ; handles digits at ten millions and hundred millions places ( if any ) ; handles digits at hundred thousands and one millions places ( if any ) ; handles digits at thousands and tens thousands places ( if any ) ; handles digit at hundreds places ( if any ) ; handles digits at ones and tens places ( if any ) ; Driver code ; long handles upto 9 digit no change to unsigned long long int to handle more digit number ; convert given number in words
#include <iostream> NEW_LINE using namespace std ; string one [ ] = { " " , " one ▁ " , " two ▁ " , " three ▁ " , " four ▁ " , " five ▁ " , " six ▁ " , " seven ▁ " , " eight ▁ " , " nine ▁ " , " ten ▁ " , " eleven ▁ " , " twelve ▁ " , " thirteen ▁ " , " fourteen ▁ " , " fifteen ▁ " , " sixteen ▁ " , " seventeen ▁ " , " eighteen ▁ " , " nineteen ▁ " } ; string ten [ ] = { " " , " " , " twenty ▁ " , " thirty ▁ " , " forty ▁ " , " fifty ▁ " , " sixty ▁ " , " seventy ▁ " , " eighty ▁ " , " ninety ▁ " } ; string numToWords ( int n , string s ) { string str = " " ; if ( n > 19 ) str += ten [ n / 10 ] + one [ n % 10 ] ; else str += one [ n ] ; if ( n ) str += s ; return str ; } string convertToWords ( long n ) { string out ; out += numToWords ( ( n / 10000000 ) , " crore ▁ " ) ; out += numToWords ( ( ( n / 100000 ) % 100 ) , " lakh ▁ " ) ; out += numToWords ( ( ( n / 1000 ) % 100 ) , " thousand ▁ " ) ; out += numToWords ( ( ( n / 100 ) % 10 ) , " hundred ▁ " ) ; if ( n > 100 && n % 100 ) out += " and ▁ " ; out += numToWords ( ( n % 100 ) , " " ) ; return out ; } int main ( ) { long n = 438237764 ; cout << convertToWords ( n ) << endl ; return 0 ; }