Mean of an Array in Java
The average
of all the numbers in the array is referred to as the mean. In other words, it
is the total of all the numbers in the array divided by the total number of
numbers.
Get an
array of Integers arr[] with size n and calculate the mean value of arr[]
Example
Input 1:
5
1 2 3 4 5
Output 1:
3
Explanation:
(1+2+3+4+5
= 15/5 = 3)
Try your Solution
Strongly recommended to Solve it on your own, Don't directly go to the solution given below.
Program or Solution
import java.util.*;
class Mean
{
public static void main(String args[])
{
//Variable and Object Declarations
Scanner input = new Scanner(System.in);
int n;
float sum = 0f, mean;
//Getting size of Array & Declare Array arr[]
n = input.nextInt();
int arr[] = new int[n];
//Get n values to array arr[]
for(int i = 0; i<n; i++)
{
arr[i] = input.nextInt();
}
//Calcualte Sum and Mean
for(int i = 0; i<n; i++)
{
sum+= arr[i];
}
mean = sum/n;
System.out.print(mean);
}
}