Beginners Programs :

Java Program To Calculate Average Of N Numbers

Java program to calculate average of N numbers. Here is the code to calculate 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 define method.

How to calculate average of N Numbers ?

The calculation of average of N numbers ins quite simple: Here is the formula for it. Hope you get understand-

java program to calculate average

The Average has been calculated as : sum of all data values / Number of data values.

The following program can be applicable for either average of two numbers or average of three numbers, or 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 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

There you go, the basic universal syntax for the average of N Numbers program or given 2 or 3 numbers as you wish.


2. Taking inputs through scanner class

Java code for obtaining average of numbers taking inputs through scanner class.


3. Using command Line Arguments 

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.



4. Using User Define Method

By using user define method. You can do allocate whatever method. This is just for an example program.



5. Through creating a separate class

Java program for getting  average of numbers through creating a separate class( ) and taking inputs through scanner class

Share your ideas and thoughts for the above program.

#Happy Coding#