-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathArray Mean and Median
More file actions
35 lines (29 loc) · 870 Bytes
/
Array Mean and Median
File metadata and controls
35 lines (29 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Java program to find median
import java.util.*;
class MeanMedian {
// Function for calculating median
public static double findMedian(int a[], int n)
{
// First we sort the array
Arrays.sort(a);
// check for even case
if (n % 2 != 0)
return (double)a[n / 2];
return (double)(a[(n - 1) / 2] + a[n / 2]) / 2.0;
}
public static double findMean(int a[], int n)
{
double sum = 0;
for(int i=0; i<n; i++)
sum+=a[i];
return (double)(sum / n);
}
// Driver program
public static void main(String args[])
{
int a[] = { 1, 2, 100, 2, 7, 5, 8, 6 };
int n = a.length;
System.out.println("Median = " + findMedian(a, n));
System.out.println("Mean = " + findMean(a, n));
}
}