Answer to Question #20271 in Java | JSP | JSF for Zach

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
1
Expert's answer
2012-12-10T10:36:08-0500
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);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS