Tuesday, 23 August 2016

Java program to find Mean Median Mode

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Solution {
public static double findMean(double array[]){
double sum = 0;
double average=0;
double length=array.length;
for(int j = 0; j < length; j++){
sum += array[j];
average = sum / length;
}
return average;
}
public static double findMedian(double array[]) {
int length=array.length;
double[] sort = new double[length];
System.arraycopy(array, 0, sort, 0, sort.length);
Arrays.sort(sort);
if (length % 2 == 0) {
return (sort[(sort.length / 2) - 1] + sort[sort.length / 2]) / 2;
} else {
return sort[sort.length / 2];
}
}

public static double findMode(double array[]) {
int length=array.length;
double[] sort = new double[length];
System.arraycopy(array, 0, sort, 0, sort.length);
Arrays.sort(sort);
double min=sort[0];
return min;
    }
public static void main(String[] args)
{
Scanner nums = new Scanner(System.in);
    int n=nums.nextInt();
double[] num = new double[n];
for ( int i = 0; i <n ; i++) {
  num[i] = nums.nextDouble();
}
double mean=Solution.findMean(num);
System.out.println(+mean);
double median=Solution.findMedian(num);
System.out.println(+median);
double mode=Solution.findMode(num);
System.out.println(+(int)mode);
}
}

OUTPUT:-


Post a Comment