Home » Java programs » Java Array programs

Java program to find average of all array elements



In this java program, we are going to read N integer elements and finding their average.
Submitted by IncludeHelp, on November 30, 2017

Given an array of integers and we have to find their average using java program.

Example:

    Input array elements: 65, 45, 25, 65, 84, 74, 96, 74, 15, 36
    Output:
    SUM of all elements: 579
    Average of all elements: 57.9

Program to find average of all array elements in java

import java.util.*; 

public class FindAverage
{
	public static void main(String[] args) 
    {
		// declare and initialize here.
        int n,sum = 0;
        float average;
        
        // create object.
        Scanner s = new Scanner(System.in);
   
        // enter total number of elements you want.
        System.out.print("Enter number of elements you want in array : ");
        n = s.nextInt();
        int a[] = new int[n];
        
        // enter elements.
        System.out.println("Enter all the elements : ");
        for(int i = 0; i < n ; i++)
        {
            a[i] = s.nextInt();
            sum = sum + a[i];
        }

        // display sum of elements.
        System.out.print("Sum of the array elements is : " +sum+"\n");
        average = (float)sum / n;
        // display average of the elements.
        System.out.print("Average of the array elements is : " +average);
    }
}

Output

    Enter number of elements you want in array : 10
    Enter all the elements : 
    65
    45
    25
    65
    84
    74
    96
    74
    15
    36
    Sum of the array elements is : 579
    Average of the array elements is : 57.9





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.