# Field Manual for Hackers

## java | Find the median of a given list of number.

1. This comment has been removed by the author.

2. import java.util.Scanner;
class ArrayMedian
{
public static void main(String args[])
{int temp,index=0;
Scanner scan=new Scanner(System.in);
System.out.println("How many number you want to add");
int n=scan.nextInt();
int ar[]=new int[n];
System.out.println("Enter Numbers");
for (int i=0;i<n;i++)
{
ar[i]=scan.nextInt();
}
for(int j=0;j<n;j++)
{
for(int i=0;i<n;i++)
{
if(ar[j]<ar[i])
{
temp=ar[j];
ar[j]=ar[i];
ar[i]=temp;
}
}
}
System.out.println("Sorted array ");
for (int i=0;i<n;i++)
{
System.out.print(ar[i]+" ");
}
if(n%2==0){System.out.println("The Median is "+ar[(n/2)-1]+" and "+ar[n/2]);}
else {System.out.println("The Median is "+ar[n/2]);}
}
}

3. import java.util.Scanner;
class D
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
int n;
System.out.println("Enter the value of n");
n=obj.nextInt();
int arr[]=new int[n];
int i,j;
System.out.println("Enter the array");
for(i=0;i<n;i++)
{
arr[i]=obj.nextInt();
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("The array after sorting is=");
for(i=0;i<n;i++)
{
System.out.println(arr[i]);
}
if(n%2==0)
{
System.out.println("The median is"+arr[(n/2)-1]+"and"+arr[n/2]);
}
else
{
System.out.println("The median is"+arr[n/2]);
}
}
}

4. import java.util.Scanner;

class median
{
public static void main(String args[])
{
float temp;
Scanner scan=new Scanner(System.in);
System.out.println("How many number you want to add");
int n=scan.nextInt();
float ar[]=new float[n];
float median;
System.out.println("Enter elements in array");
for (int i=0;i<n;i++)
{
ar[i]=scan.nextFloat();
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(ar[i]<ar[j])
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}
}
System.out.println("New array after sorting ");
for (int i=0;i<n;i++)
{
System.out.print(ar[i]+" ");
}
if(n%2==0)
{
float sum = ar[n/2] + ar[(n/2)-1] ;
median = sum/2;
System.out.println( "The median is"+median);
}
else

{
System.out.println("The median is"+ar[n/2]);
}

}
}

OUTPUT:

How many number you want to add
6
Enter elements in array
12
6
45
48
12
65
New array after sorting
6.0 12.0 12.0 45.0 48.0 65.0 The median is28.5

5. import java.util.Scanner;
class Median
{
public static void main(String Args[])
{
System.out.println("How many numbers you want to enter?");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int temp,i,j;
int AR[]=new int[n];
System.out.println("enter the values");
for( i=0;i<n;i++)
{
AR[i]=sc.nextInt();
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(AR[i]<AR[j])
{
temp=AR[i];
AR[i]=AR[j];
AR[j]=temp;
}
}
}
System.out.println("sorting array:");
for(i=0;i<n;i++)
{
System.out.println(AR[i]+"");

}
if(n%2==0)
System.out.println("median is "+(AR[(n/2)-1]+AR[(n/2)])/2);
else
System.out.println("median is "+AR[n/2]);
}}

6. import java.util.Scanner;
class Median{
public static void main(String ar[]){
Scanner obj=new Scanner(System.in);
int n,i,flag,j;
float median;
System.out.println("enter the value of n");
n=obj.nextInt();
int arr[]=new int[n];
for(i=0;iarr[j]){
flag=arr[i];
arr[i]=arr[j];
arr[j]=flag;
}
}
}
for(i=0;i<n;i++)
System.out.println(arr[i]);
System.out.println("the above array is sorted");

if(n%2==0){
median=(arr[(n/2)-1]+arr[n/2])/2;
System.out.println("median is"+median);
}
else
{
median=arr[(n-1)/2];
System.out.println("median is"+median);
}

}

}

7. import java.util.Scanner;
class Median{
public static void main(String args[]){
int n,i=0,j=0;
double m,temp=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the size of array:");
n=scan.nextInt();
double ar[]=new double[n];
for(i=0;i<n;i++)
{
System.out.println("Enter the value"+(i+1));
ar[i]=scan.nextInt();
}
System.out.println("Array :");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(ar[i]<ar[j])
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}

}
System.out.println("Sorted array:");
for(i=0;i<n;i++)
System.out.println(+ar[i]);

if(n%2==0)
{
m=(ar[(n/2)-1]+ar[n/2])/2;
System.out.println("the median is"+m);
}
else
System.out.println("the median is"+(ar[n/2]));

}
}

8. import java.util.Scanner;
class Median{
public static void main(String a[]){
int n,temp=0;
double median;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the size:");
n=scan.nextInt();
int ar[]=new int[n];
System.out.println("Enter the values:");
for(int i=0;i<n;i++){
ar[i]=scan.nextInt();
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(ar[i]<ar[j]){
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}
}
if(n%2==0){
median=(ar[n/2]+ar[(n/2)-1])/2;
}
else{
median=ar[n/2];
}
System.out.println("The median is:"+median);
}
}