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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.