# How to calculate Average of numbers in Java ?.

Updated on September 17, 2014

In this Java tutorial, you will learn how to calculate average of numbers entered by the user on the Java console. In this tutorial, there is a Java class by name 'AverageDemo', having a main method. This main method gets executed when you run Java program. Average of numbers is calculated by the formula -

Average of numbers = Sum of the numbers / total numbers.

Below is the Java source code and Java video tutorial, which calculates average of numbers.

## Steps to implement -

Step 1 - Import Scanner Java class in package 'java.util'. By importing Scanner class, a class can use methods of Scanner class such as, nextDouble and nextInt.

Step 2 - Create Scanner class instance by name input. The Scanner class constructor takes in instance of System.in. It helps Scanner class to read user inputs on the Java console.

Step 3 - Prompt the user to enter number of elements for which user wants average.

Step 4 - Using scanner class nextInt method, user input is assigned to noOfElements.

Step 5 - Create an array of double type by name numbers. The numbers variable takes in length as value in noOfElements. This array will hold numbers entered by user.

Step 6 - Create a double variable named as sum. Assign it value as 0.

Step 7 - Using for loop, ask user to enter noOfElements times number and calculate sum by iterating each number.

Step 8 - Calculate average by dividing sum by noOfElements.

Step 9 - Printing average in the console.

A simple Java video tutorial to demonstrate how to calculate Average of numbers entered by the user on the Java console.

Below is the complete Java source code which demonstrate how to calculate Average of numbers entered by the user on the Java console.

```// Step 1 - Import Scanner Java class in package
// 'java.util'. By importing Scanner class, a class
// can use methods of Scanner class such as, nextDouble
// and nextInt.
import java.util.Scanner;

public class AverageDemo {

public static void main(String[] args) {

// Step 2 - Create Scanner class instance by name input.
// The Scanner class constructor takes in instance of
// System.in. It helps Scanner class to read user inputs
// on the Java console.
Scanner input = new Scanner(System.in);

// Step 3 - Prompt the user to enter number of elements for
// which user wants average.
System.out.print("Enter total numbers to find average : ");

// Step 4 - Using scanner class nextInt method, user input
// is assigned to noOfElements
int noOfElements = input.nextInt();

// Step 5 - Create an array of double type by name numbers. The
// numbers variable takes in length as value in noOfElements.
// This array will hold numbers entered by user.
double[] numbers = new double[noOfElements];

// Step 6 - Create a double variable named as sum.
// Assign it value as 0.
double sum = 0;

// Step 7 - Using for loop, ask user to enter noOfElements times
// number and calculate sum by iterating each number.
for (int i = 0; i < noOfElements; i++) {
System.out.print("Enter number "+ (i+1) + " : " );
numbers[i] = input.nextDouble();
sum += numbers[i];
}

// Step 8 - Calculate average by dividing sum by noOfElements.
double average = sum / noOfElements;

System.out.println("");

// Step 9 - Printing average in the console.
System.out.println("Average is " + average);
}

}
```

Output of the above Java program -

0

0

5