Question #20271

Write a JAVA program that takes as input five numbers and outputs the mean
(average) and standard deviation of the numbers. If the numbers are x1, x2,
x3, x4, and x5, then the mean is x ¼ (x1 + x2 + x3 + x4 + x5)/5 and the
standard deviation is:
Your program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation.
format the output to 2 decimal places

Expert's answer

import java.util.Scanner;

public class Question20271 {

private static Scanner input =new Scanner(System.in);

private static double[] numbers=new double[100];

public static void main(String[] args) {

for(int i=0;i<5;i++){

System.out.print("Enter number "+(i+1)+": ");

numbers[i]=input.nextDouble();

}

System.out.println("Mean= "+CalculateMean());

System.out.println("Standard Deviation= "+CalculateStandardDeviation());

}

private static double CalculateMean(){

double sum=0;

for(int i=0;i<5;i++){

sum+=numbers[i];

}

return sum/5;

}

private static double CalculateStandardDeviation(){

double sum=0;

for(int i=0;i<5;i++){

sum+=Math.pow(numbers[i],2);

}

return Math.sqrt(sum);

}

}

public class Question20271 {

private static Scanner input =new Scanner(System.in);

private static double[] numbers=new double[100];

public static void main(String[] args) {

for(int i=0;i<5;i++){

System.out.print("Enter number "+(i+1)+": ");

numbers[i]=input.nextDouble();

}

System.out.println("Mean= "+CalculateMean());

System.out.println("Standard Deviation= "+CalculateStandardDeviation());

}

private static double CalculateMean(){

double sum=0;

for(int i=0;i<5;i++){

sum+=numbers[i];

}

return sum/5;

}

private static double CalculateStandardDeviation(){

double sum=0;

for(int i=0;i<5;i++){

sum+=Math.pow(numbers[i],2);

}

return Math.sqrt(sum);

}

}

## Comments

## Leave a comment