File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Java/Algorithms/Mathematical Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments