# 1. Introduction

In this tutorial, we’ll create a Java program to calculate the average of a set of numbers using arrays. Arrays in Java work as containers that hold a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

# 2. Program Steps

1. Define the main class named *AverageCalculator*.

2. Inside the main class, define the *main* method.

3. Inside the *main* method, declare an array of numbers for which we want to calculate the average.

4. Calculate the sum of the numbers in the array.

5. Calculate the average by dividing the sum by the length of the array.

6. Print the calculated average.

# 3. Code Program

```
public class AverageCalculator { // 1. Defining the main class
public static void main(String[] args) { // 2. Defining the main method
// 3. Declaring an array of numbers
int[] numbers = {10, 20, 30, 40, 50};
int sum = 0; // Initializing sum
// 4. Calculating the sum of the numbers in the array
for (int number : numbers) {
sum += number;
}
// 5. Calculating the average
double average = (double) sum / numbers.length;
// 6. Printing the average
System.out.println("The average of the given numbers is: " + average);
}
}
```

### Output:

The average of the given numbers is: 30.0

# 4. Step By Step Explanation

– Step 1: The main class named *AverageCalculator* is defined.

– Step 2: The *main* method, which is the entry point of the program, is defined inside the main class.

– Step 3: Inside the *main* method, we declare an array *numbers* containing the elements *{10, 20, 30, 40, 50}* for which we want to calculate the average.

– Step 4: We initialize a variable *sum* to *0* and then iterate over each number in the *numbers* array, adding it to *sum*.

– Step 5: We calculate the *average* by dividing the *sum* by the length of the *numbers* array. We cast *sum* to *double* to perform floating-point division and get a more accurate result.

– Step 6: Finally, the program prints the calculated average *30.0* to the console.