Example of Average in Java Program
Java program to calculate the average of N numbers. Here is the code to calculate the average of N numbers or average of 2 or 3 numbers. The following code has been written in three different ways, using standard values, using do while , recursion, command line arguments, creating a separate class, user-defined method.
How to calculate the average of N Numbers?
The calculation of the average of N numbers ins quite simple: Here is the formula for it. Hope you get to understand-
The Average has been calculated as the sum of all data values / Number of data values.
The following program can be applied for either average of two numbers or average of three numbers, or an average of N numbers. In the case of all N number. Just replace the SOP with the above-given formula.
The following code has been written in five different ways for both programs along with the sample output for an average of 2 numbers and average of three numbers or N numbers, in using standard values , while loop, for loop, do while loop, command line arguments, user define method, creating a separate class.
1. Java Program Calculate Average Using Standard values
[wp_ad_camp_3]
There you go, the basic universal syntax for the average of N Numbers program or given 2 or 3 numbers as you wish.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Average { public static void main ( String arg [ ] ) { int n = 5 , result = 0 ; int a [ ] = new int [ 5 ] ; a [ 0 ] = 10 ; a [ 1 ] = 20 ; a [ 2 ] = 30 ; a [ 3 ] = 40 ; a [ 4 ] = 50 ; for ( int i = 0 ; i < n ; i ++ ) result = result + a [ i ] ; System . out . println ( "average of (" + a [ 0 ] + "," + a [ 1 ] + "," + a [ 2 ] + "," + a [ 3 ] + "," + a [ 4 ] + ") is =" + result / n ) ; } } |
output:
average of ( 10 , 20 , 30 , 40 , 50 ) is = 30 |
2. Taking inputs through scanner class
Java code for obtaining an average of numbers taking inputs through scanner class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | java . util . Scanner ; class Average { public static void main ( String arg [ ] ) { int n ; double res = 0 ; Scanner sc = new Scanner ( System . in ) ; System . out . println ( "enter how many numbers to cal avg" ) ; n = sc . nextInt ( ) ; int a [ ] = new int [ n ] ; System . out . println ( "enter " + n + " numbers" ) ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) res = res + a [ i ] ; System . out . println ( "average=" + res / n ) ; } } |
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | enter how many numbers to cal avg 6 enter 6 numbers 1 2 3 4 5 6 average = 3.5 enter how many numbers to cal avg 4 enter 4 numbers 12 5 44 15 average = 19.0 |
3. Using Command Line Arguments
[wp_ad_camp_3]
By using command line arguments: If you have no idea about command line arguments then do check out our guide here – what are command line arguments in java with examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | java . util . Scanner ; class Average { public static void main ( String args [ ] ) { long n , p ; int i ; double res = 0 ; n = args . length ; for ( i = 0 ; i < n ; i ++ ) res = res + Integer . parseInt ( args [ i ] ) ; System . out . print ( "Average of (" ) ; for ( i = 0 ; i < n - 1 ; i ++ ) System . out . print ( args [ i ] + "," ) ; System . out . println ( args [ i ] + ")=" + res / n ) ; } } |
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ( 1 ) javac Average . java java Average 1 2 3 4 5 Average of ( 1 , 2 , 3 , 4 , 5 ) = 3.0 2 ) java Average 1 2 3 4 5 6 7 8 9 10 Average of ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) = 5.5 3 ) java Average 25 52 35 52 111 Average of ( 25 , 52 , 35 , 52 , 111 ) = 55.0 4 ) java Average 12 34 56 78 91 23 45 Average of ( 12 , 34 , 56 , 78 , 91 , 23 , 45 ) = 48.42857142857143 |
4. Using the User Defined Method
By using the user-defined method. You can do allocate whatever method. This is just for an example program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | java . util . Scanner ; class Average { public static void main ( String args [ ] ) { int n ; double res = 0 ; Scanner sc = new Scanner ( System . in ) ; System . out . println ( "enter how many numbers to cal avg" ) ; n = sc . nextInt ( ) ; int a [ ] = new int [ n ] ; System . out . println ( "enter " + n + " numbers" ) ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = sc . nextInt ( ) ; res = Average . CalAvg ( a , n ) ; System . out . println ( "average=" + res / n ) ; } static double CalAvg ( int a [ ] , int n ) { double res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = res + a [ i ] ; return res ; } } |
output:
enter how many numbers to cal avg 6 enter 6 numbers 11 21 31 41 51 61 average = 36 |
5. Through creating a separate class
Java program for getting average of numbers through creating a separate class( ) and taking inputs through scanner class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | java . util . Scanner ; class Avg { double res ; Avg ( int a [ ] , int n ) { res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = res + a [ i ] ; } } class Average { public static void main ( String args [ ] ) { int n ; double res = 0 ; Scanner sc = new Scanner ( System . in ) ; System . out . println ( "enter how many numbers to cal avg" ) ; n = sc . nextInt ( ) ; int a [ ] = new int [ n ] ; System . out . println ( "enter " + n + " numbers" ) ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = sc . nextInt ( ) ; Avg k = new Avg ( a , n ) ; System . out . println ( "Average =" + k . res ) ; } } |
Share your ideas and thoughts for the above program.
#Happy Coding#
Source: https://javatutoring.com/java-program-calculate-average-numbers/
0 Response to "Example of Average in Java Program"
Post a Comment