Java Program to Calculate Average Using Arrays

Program to Calculate Average Using Arrays in Java

What are Arrays?

In Java, an array is a container object that holds a fixed number of values of a single type. The values can be of any type, including primitive types (such as int, char, and boolean) and reference types (such as objects).

In this Article, we will write a program to calculate average of the values of an array.

Array Class:

In Java, the java.util.Arrays class is a utility class that provides various methods for working with arrays. Here are some examples of the methods that are available in the Arrays class:

  • sort: sorts the elements of an array in ascending order.
  • binarySearch: searches for a specified element in an array using binary search.
  • fill: fills an array with a specified value.
  • equals: checks if two arrays are equal.

Average of an Array :

To calculate the average of the elements in an array in Java, you can use a loop to iterate over the elements of the array and sum them up, and then divide the sum by the length of the array.

Pseudo Code :

int[] numbers = {1, 2, 3, 4, 5};

double sum = 0;
for (int i = 0; i < numbers.length; i++) {
   sum += numbers[i];
}

double average = sum / numbers.length;
System.out.println(average); 
Output:

3.0

Explanation:

This code iterates over the elements of the numbers array using a loop, adds up all the elements, and then divides the sum by the length of the array to calculate the average. The average is then printed to the console.

Example Program to calculate Average using Arrays :

Run
public class Main {
   public static void main(String[] args) {
      int[] numbers = {1, 2, 3, 4, 5};
      double average = calculateAverage(numbers);
      
      // prints the average 
      System.out.println("The average is: " + average);  
   }

   public static double calculateAverage(int[] numbers) {
      double sum = 0;
      
      // Calculating the average of an array
      for (int i = 0; i < numbers.length; i++) {
         sum += numbers[i];
      }
      
      // returning the average to the main class
      return sum / numbers.length;
   }
}
Output:

The average is: 3.0

Explanation:

This code defines a method calculateAverage that takes an array of integers and returns the average of those numbers. The method uses a loop to iterate over the elements of the array, adds up all the elements, and then divides the sum by the length of the array to calculate the average.

The main method creates an array of integers and passes it to the calculateAverage method, which calculates the average and prints it to the console.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription