## Friday, 19 August 2016

### Chapter 5 Exercise 45, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*5.45 (Statistics: compute mean and standard deviation) In business applications, you are often asked to compute the mean and standard deviation of data. The mean is simply the average of the numbers. The standard deviation is a statistic that tells you how tightly all the various data are clustered around the mean in a set of data. For example, what is the average age of the students in a class? How close are the ages? If all the students are the same age, the deviation is 0. Write a program that prompts the user to enter ten numbers, and displays the mean and standard deviations of these numbers using the following formula: mean=ni=1xin$mean=\frac{\sum _{i=1}^{n}{x}_{i}}{n}$ deviation=ni=1x2i(ni=1xi)2nn1$deviation=\sqrt{\frac{\sum _{i=1}^{n}{x}_{i}^{2}-\frac{\left(\sum _{i=1}^{n}{x}_{i}{\right)}^{2}}{n}}{n-1}}$

Here is a sample run:

Enter ten numbers: 1 2 3 4.5 5.6 6 7 8 9 10
The mean is 5.61
The standard deviation is 2.99794
import java.util.Scanner;

public class ProgrammingEx5_45 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Enter saving amount
System.out.print("Enter ten numbers:");
double sum = 0;
double sumsq = 0;

for (int i = 0; i < 10; i++) {
double n = input.nextDouble();
sum += n;
sumsq += Math.pow(n, 2);

}

System.out.println("The mean is " + sum / 10);
System.out.println("The standard deviation is "
+ Math.sqrt(((sumsq - Math.pow(sum, 2) / 10))/9));

}

}