Skip to content

Commit 142d7cd

Browse files
authored
Median.java div-bargali#794
Median.java
2 parents 73768b1 + 31fc22d commit 142d7cd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.lang.*;
4+
class Median
5+
{
6+
public static void main(String args[])
7+
{
8+
Scanner sc=new Scanner(System.in);
9+
System.out.println("Enter the number of elements in the array");
10+
int n=sc.nextInt();
11+
double[] input=new double[n];
12+
System.out.println("Enter the elements of the array(" +n+ "): ");
13+
for(int i=0;i<n;i++)
14+
{
15+
input[i]=sc.nextDouble();
16+
}
17+
double res = calcMedian(n,input);
18+
System.out.println("The median of the array is : " + res);
19+
}
20+
static double calcMedian(int n, double arr[])
21+
{
22+
double k=0;
23+
if(n%2==1)
24+
{
25+
k = arr[((n+1)/2)-1];
26+
}
27+
else
28+
{
29+
k = (arr[n/2-1]+arr[n/2])/2;
30+
}
31+
return k;
32+
}
33+
}

0 commit comments

Comments
 (0)