Average and Numbers Greater than Average in Array Java

Program description:- Write a Java program to find the average of an array and numbers greater than the average in an array.

Example1:-
Array = {10, 20, 30, 40, 50}
Average = 30.0
Numbers greater than average = 40, 50

Example2:-
Array = {9, 8, 7, 0, -2, 5}
Average = 4.0
Numbers greater than average = 9, 8, 7, 5

Example3:-
Array = {-10, 5, 0, -9, 18, 27, -36}
Average = 0.0
Numbers greater than average = 5, 18, 27

Procedure to develop Java method to find the average,
a) Take an array.
b) Declare a variable to store the sum value. Initialize it with 0.
c) Traverse through the array.
d) Add each element to the sum variable.
e) Find average as, average = sum / length of array.

Procedure to develop Java method to find the numbers greater than average,
a) Take an array and average of the array.
b) Traverse through the array.
c) If the element is greater than average then display it.

Prerequisite:- Array in Java, find length of array in Java, sum of array in Java, average of the array in Java

In this program, we will take input value for the array from the end-user, but you can take it explicitly. See:- how to take array input in Java. We will use a method to perform the operation. 

Java Program to Find Average and Numbers greater than Average in Array

import java.util.Scanner;

public class ArrayTest {

  public static void main(String[] args) {
    // create Scanner class object to take input
    Scanner scan = new Scanner(System.in);

    // read size of the array
    System.out.print("Enter size of the array: ");
    int n = scan.nextInt();

    // create an int array of size N
    int numbers[] = new int[n];

    // take input for the array
    System.out.println("Enter array elements: ");
    for (int i = 0; i < n; ++i) {
      numbers[i] = scan.nextInt();
    }

    // find average value
    double avg = average(numbers);
    
    // display average
    System.out.println("Average = " + avg);
    
    // display numbers greater then average
    System.out.println("Numbers greater then average = ");
    display(numbers, avg);

    // close Scanner
    scan.close();
  }

  // method to display numbers greater than average
  public static void display(int[] numbers, double avg) {
    // traverse through the array
    for (int i : numbers) {
      if(i > avg)
      System.out.print(i+" ");
    }
    
  }

  // method to find average of the array
  public static double average(int[] numbers) {
    // variable
    int sum = 0;

    // traverse through the array
    for (int i = 0; i < numbers.length; i++) {
      sum += numbers[i]; // find sum
    }
    
    // return average value
    return sum/numbers.length;
  }
}

Output:-

Enter size of the array: 5
Enter array elements:
10 20 30 40 50
Average = 30.0
Numbers greater then average =
40 50

Enter size of the array: 6
Enter array elements:
9 8 7 0 -2 5
Average = 4.0
Numbers greater then average =
9 8 7 5

Enter size of the array: 7
Enter array elements:
-10 5 0 -9 18 27 -36
Average = 0.0
Numbers greater then average =
5 18 27

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *