![Program to print number to words in java Program to print number to words in java](/uploads/1/2/6/0/126032942/297335669.jpg)
Java tutorial Interview Questions Java examples Servlet tutorial JSP tutorial Jdbc tutorial JSON Struts tutorial Hibernate tutorial Spring MVC Jersey JAX-RS tutorial Design Pattern Quartz Scheduler MongoDB Tutorial All Post. Write a program to print amount in English.i want optimum solution. Input: Any number up to 1 million. Output: Pronunciation of the input number in English.
Game starcraft 3 full crack. Write code to convert a given number into words. For example, if “1234” is given as input, output should be “one thousand two hundred thirty four”.
Following is the implementation for the same. The code supports numbers up-to 4 digits, i.e., numbers from 0 to 9999. Idea is to create arrays that store individual parts of output strings. One array is used for single digits, one for numbers from 10 to 19, one for 20, 30, 40, 50, . etc, and one for powers of 10.
The given number is divided in two parts: first two digits and last two digits, and the two parts are printed separately.
The given number is divided in two parts: first two digits and last two digits, and the two parts are printed separately.
C
/* C program to print a given number in words. The program handles #include <stdio.h> #include <stdlib.h> /* A function that prints given number in words */ { int len = strlen (num); // Get number of digits in given number /* Base cases */ fprintf (stderr, 'empty stringn' ); } fprintf (stderr, 'Length more than 4 is not supportedn' ); } /* The first string is not used, it is to make char *single_digits[] = { 'zero' , 'one' , 'two' , 'six' , 'seven' , 'eight' , 'nine' }; /* The first string is not used, it is to make char *two_digits[] = { ' , 'ten' , 'eleven' , 'twelve' , 'fifteen' , 'sixteen' , /* The first two string are not used, they are to make char *tens_multiple[] = { ' , ' , 'twenty' , 'thirty' , 'forty' , 'fifty' , printf ( 'n%s: ' , num); /* For single digit number */ printf ( '%sn' , single_digits[*num - '0' ]); } /* Iterate while num is not '0' */ if (len >= 3) { printf ( '%s ' , single_digits[*num - '0' ]); printf ( '%s ' , tens_power[len-3]); // here len can be 3 or 4 --len; else { /* Need to explicitly handle 10-19. Sum of the two digits is if (*num '1' ) { printf ( '%sn' , two_digits[sum]); } /* Need to explicitely handle 20 */ printf ( 'twentyn' ); } /* Rest of the two digit numbers i.e., 21 to 99 */ int i = *num - '0' ; ++num; printf ( '%s ' , single_digits[*num - '0' ]); } } int main( void ) convert_to_words( '9923' ); convert_to_words( '89' ); } |
Java
// Java program to print a given number in words. The { // given number in words { // in given number if (len 0 ) System.out.println( 'empty string' ); } { System.out.println( 'Length more than 4 is not supported' ); } /* The first string is not used, it is to make String[] single_digits = new String[]{ 'zero' , 'one' , 'five' , 'six' , 'seven' , array indexing simple */ String[] two_digits = new String[]{ ' , 'ten' , 'eleven' , 'twelve' , 'fifteen' , 'sixteen' , 'seventeen' , /* The first two string are not used, they are to make array indexing simple*/ String[] tens_multiple = new String[]{ ' , ' , 'twenty' , 'thirty' , 'forty' , 'eighty' , 'ninety' }; String[] tens_power = new String[] { 'hundred' , 'thousand' }; /* Used for debugging purpose only */ if (len 1 ) System.out.println(single_digits[num[ 0 ] - '0' ]); } /* Iterate while num int x = 0 ; { /* Code path for first 2 digits */ { { System.out.print(single_digits[num[x] - '0' ]+ ' ' ); // here len can be 3 or 4 --len; else /* Need to explicitly handle is used as index of 'two_digits' if (num[x] - '0' 1 ) int sum = num[x] - '0' + System.out.println(two_digits[sum]); } /* Need to explicitely handle 20 */ num[x + 1 ] - '0' 0 ) System.out.println( 'twenty' ); } /* Rest of the two digit else int i = (num[x] - '0' ); System.out.print(tens_multiple[i]+ ' ' ); System.out.print( ' ); if (num[x] - '0' != 0 ) } ++x; } // Driver Code { convert_to_words( '523' .toCharArray()); convert_to_words( '8989' .toCharArray()); } // by Mithun Kumar |
Python3
# words. The program handles numbers # given number in words # in given number if (l = = 0 ): return ; if (l > 4 ): return ; # The first string is not used, single_digits = [ 'zero' , 'one' , 'two' , 'three' , 'eight' , 'nine' ]; # The first string is not used, two_digits = [' ', ' ten ', ' eleven ', ' twelve', 'sixteen' , 'seventeen' , 'eighteen' , # they are to make array indexing simple tens_multiple = [' ', ' ', ' twenty ', ' thirty ', ' forty', 'ninety' ]; tens_power = [ 'hundred' , 'thousand' ]; # Used for debugging purpose only if (l = = 1 ): return ; # Iterate while num is not '0' while (x < len (num)): # Code path for first 2 digits if ( ord (num[x]) - 48 ! = 0 ): end = ' ' ); # here len can be 3 or 4 l - = 1 ; # Code path for last 2 digits # 10-19. Sum of the two digits # array of strings sum = ( ord (num[x]) - 48 + print (two_digits[ sum ]); elif ( ord (num[x]) - 48 = = 2 and print ( 'twenty' ); # numbers i.e., 21 to 99 i = ord (num[x]) - 48 ; print (tens_multiple[i], end = ' ' ); print (' ', end = ' '); if ( ord (num[x]) - 48 ! = 0 ): x + = 1 ; # Driver Code convert_to_words( '523' ); convert_to_words( '8989' ); # This code is contributed |
C#
// number in words. The program using System; class GFG // A function that prints static void convert_to_words( char [] num) // Get number of digits int len = num.Length; // Base cases { return ; if (len > 4) Console.WriteLine( 'Length more than ' + return ; it is to make array indexing simple */ string [] single_digits = new string []{ 'zero' , 'one' , 'two' , 'six' , 'seven' , 'eight' , it is to make array indexing simple */ string [] two_digits = new string []{ ' , 'ten' , 'eleven' , 'twelve' , 'fifteen' , 'sixteen' , 'seventeen' , they are to make array indexing simple*/ string [] tens_multiple = new string []{ ' , ' , 'twenty' , 'thirty' , 'seventy' , 'eighty' , 'ninety' }; string [] tens_power = new string [] { 'hundred' , 'thousand' }; /* Used for debugging purpose only */ if (len 1) Console.WriteLine(single_digits[num[0] - '0' ]); } /* Iterate while num int x = 0; { /* Code path for first 2 digits */ { { Console.Write(single_digits[num[x] - '0' ] + ' ' ); } } /* Code path for last 2 digits */ { 10-19. Sum of the two digits array of strings */ { num[x] - '0' ; return ; else if (num[x] - '0' 2 && { return ; numbers i.e., 21 to 99 */ { if (i > 0) else ++x; Console.WriteLine(single_digits[num[x] - '0' ]); } } public static void Main() convert_to_words( '9923' .ToCharArray()); convert_to_words( '89' .ToCharArray()); } // by Mits |
PHP
// PHP program to print a given // program handles numbers // given number in words { // in given number if ( $len 0) echo 'empty stringn' ; } { 'is not supportedn' ; } /* The first string is not used, $single_digits = array ( 'zero' , 'one' , 'two' , 'six' , 'seven' , 'eight' , it is to make array indexing simple */ $two_digits = array ( ' , 'ten' , 'eleven' , 'twelve' , 'sixteen' , 'seventeen' , 'eighteen' , they are to make array indexing simple*/ $tens_multiple = array ( ' , ' , 'twenty' , 'thirty' , 'seventy' , 'eighty' , 'ninety' ); $tens_power = array ( 'hundred' , 'thousand' ); /* Used for debugging purpose only */ if ( $len 1) echo $single_digits [ $num [0] - '0' ] . ' n' ; } /* Iterate while num $x = 0; { /* Code path for first 2 digits */ { { echo $tens_power [ $len - 3] . ' ' ; } } /* Code path for last 2 digits */ { 10-19. Sum of the two digits array of strings */ { $num [ $x ] - '0' ; return ; else if ( $num [ $x ] - '0' 2 && { return ; numbers i.e., 21 to 99 */ { if ( $i > 0) else ++ $x ; echo $single_digits [ $num [ $x ] - } ++ $x ; } // Driver Code convert_to_words( '523' ); convert_to_words( '8989' ); // This code is contributed ?> |
Convert Number To Words Java
Output:This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
This is the java program to count number of words and spaces in a string. I have taken initial value of word as 1 because if there is only one word in the string then there will be no space. The total spaces will be one less than the total number of words. It is a simple program and self explanatory, still if you are facing any problem to understand this then comment below.
;
for(inti=0;i<str.length();++i)
if(str.charAt(i)' ')
}
System.out.println('Number of words='+word);
}
Navigon select keygen download.
Output
Output