Program to convert a given number to words | Java program to print a given number in words . The program handles numbers from 0 to 9999 ; A function that prints given number in words ; Get number of digits in given number ; Base cases ; The first string is not used , it is to make array indexing simple ; The first string is not used , it is to make array indexing simple ; The first two string are not used , they are to * make array indexing simple ; Used for debugging purpose only ; For single digit number ; Iterate while num is not ' \0' ; Code path for first 2 digits ; here len can be 3 or 4 ; Code path for last 2 digits ; Need to explicitly handle 10 - 19. Sum of the two digits is used as index of " two _ digits " array of strings ; Need to explicitely handle 20 ; Rest of the two digit numbers i . e . , 21 to 99 ; Driver Code
class GFG { static void convert_to_words ( char [ ] num ) { int len = num . length ; if ( len == 0 ) { System . out . println ( " empty ▁ string " ) ; return ; } if ( len > 4 ) { System . out . println ( " Length ▁ more ▁ than ▁ 4 ▁ is ▁ not ▁ supported " ) ; return ; } String [ ] single_digits = new String [ ] { " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; String [ ] two_digits = new String [ ] { " " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " } ; String [ ] tens_multiple = new String [ ] { " " , " " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " } ; String [ ] tens_power = new String [ ] { " hundred " , " thousand " } ; System . out . print ( String . valueOf ( num ) + " : ▁ " ) ; if ( len == 1 ) { System . out . println ( single_digits [ num [ 0 ] - '0' ] ) ; return ; } int x = 0 ; while ( x < num . length ) { if ( len >= 3 ) { if ( num [ x ] - '0' != 0 ) { System . out . print ( single_digits [ num [ x ] - '0' ] + " ▁ " ) ; System . out . print ( tens_power [ len - 3 ] + " ▁ " ) ; } -- len ; } else { if ( num [ x ] - '0' == 1 ) { int sum = num [ x ] - '0' + num [ x + 1 ] - '0' ; System . out . println ( two_digits [ sum ] ) ; return ; } else if ( num [ x ] - '0' == 2 && num [ x + 1 ] - '0' == 0 ) { System . out . println ( " twenty " ) ; return ; } else { int i = ( num [ x ] - '0' ) ; if ( i > 0 ) System . out . print ( tens_multiple [ i ] + " ▁ " ) ; else System . out . print ( " " ) ; ++ x ; if ( num [ x ] - '0' != 0 ) System . out . println ( single_digits [ num [ x ] - '0' ] ) ; } } ++ x ; } } public static void main ( String [ ] args ) { convert_to_words ( "9923" . toCharArray ( ) ) ; convert_to_words ( "523" . toCharArray ( ) ) ; convert_to_words ( "89" . toCharArray ( ) ) ; convert_to_words ( "8" . toCharArray ( ) ) ; } }