# How To Find The Average Of Numbers In Java

We will see in this tutorial how to find the average of numbers  and then we write java program to find the average of numbers .

## How to find the average of numbers?

The average is the outcome from the sum of the given numbers divided by the count of the numbers being averaged.

``               average = sum of all number / count of number``

Example :  1,2,3,5,9

count of number : 5

sum of all number = 20

then average = 20/5=4

Now we will write java program to find the average of numbers

1. Find the average of numbers entered by user
2. Program to calculate average of numbers using array

## Example 1 : Find the average of numbers entered by user

We calculate average of n natural numbers in this java program . We take input from user count of number then read number using scanner and store in array . First we calculate sum of numbers and to find average we divided sum of number by count of number .

``````
import java.util.Scanner;

public class AverageExample1 {

public static void main(String[] args) {

System.out.println("Enter count of numbers");
Scanner scanner = new Scanner(System.in);
/*
* take input for count from user
*/
int count = scanner.nextInt();

double[] arr = new double[count];
double sum = 0;

/*
* take element value from user
*/
for (int i = 0; i < arr.length; i++) {
System.out.print("Enter Value of Element " + (i + 1) + ":");
arr[i] = scanner.nextDouble();
}
scanner.close();
/*
* calculate sum of element
*/
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}

/*
* calculate average of element
*/
double average = sum / arr.length;

/*
* print the average of numbers
*/
System.out.println("The average is:" + average);
}
}``````

Output:

``````Enter count of numbers
5
Enter Value of Element 1:10
Enter Value of Element 2:20
Enter Value of Element 3:30
Enter Value of Element 4:45
Enter Value of Element 5:60
The average is:33.0``````

## Example 2 : Program to calculate average of numbers using array

In this program we will find the average of numbers of given array . we calculate sum of element of array then to get average , we divided sum by length of array .

``````public class AverageExample2 {
public static void main(String[] args) {

double[] arr = {10,20,30.5,40,25};
double sum = 0;

/*
*calculate sum of element of array
*/
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
//calculate  average of array
double average = sum / arr.length;
//print average if array
System.out.println("The average is:" + average);
}
}``````

Output :

``The average is:25.1``

In this tutorial we have learned how to find the average of numbers using array . We calculated average of number enter by user also . You can learn more java program for practice .