Java Program to Find the Elements from the Array which are Smaller than the Average of Array

In the previous article, we have seen Java Program to Find the Elements from the Array which are Greater than the Average of Array

In this article we are going to see how to find the elements from an array which are smaller than the average of all elements of the array.

Java Program to Find the Elements from the Array which are Smaller than the Average of Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find the elements from an array which are smaller than the average of all elements of the array.

Method-1: Java Program to Find the Elements from the Array which are Smaller than the Average of Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Find the average of all elements of the array elements.
  • Iterate over the array.
  • Check if any element is smaller than the average of array.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        //initializing sum value as 0
        int sum=0;
        int length=arr.length;
        
        //Finding average of array elements
        for (int i=0; i<arr.length;i++) 
        {
                sum=sum+arr[i];
        }
        
        //find average
        int avg=sum/length;
        System.out.println("Average of array elements : "+avg);

        System.out.print("Elements smaller than average " + avg + " are : ");
        // calling the method to find smaller elements than the average
        findSmaller(arr, avg);
    }
    
    
    //findSmaller() method which finds smaller elements than the average
    public static void findSmaller(int[] arr, int avg) 
    {
         for (int i : arr) 
         {
             if (i < avg) 
             {
                 System.out.print(i + " ");
             }

         }
     }
}
Output:

Average of array elements : 5
Elements smaller than average 5 are : 6 7 8 9 10

Method-2: Java Program to Find the Elements from the Array which are Smaller than the Average of Array By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Find the average of all elements of the array elements.
  • Iterate over the array.
  • Check if any element is smaller than the average of array.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        
        //initializing sum value as 0
        int sum=0;
        int length=arr.length;
        
        //Finding average of array elements
        for (int i=0; i<arr.length;i++) 
        {
                sum=sum+arr[i];
        }
        
        //find average
        int avg=sum/length;
        System.out.println("Average of array elements : "+avg);

        System.out.print("Elements smaller than average " + avg + " are : ");
        // calling the method to find smaller elements than the average
        findSmaller(arr, avg);
    }
    
    
    //findSmaller() method which finds smaller elements than the average
    public static void findSmaller(int[] arr, int avg) 
    {
         for (int i : arr) 
         {
             if (i < avg) 
             {
                 System.out.print(i + " ");
             }

         }
     }
}
Output:

Enter the size of array: 5
Enter array elements: 1 2 3 4 5
Average of array elements : 3
Elements smaller than average 3 are : 4 5

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: