Java Program to Calculate Standard Deviation

In this java program, we will learn about how to calculate the standard deviation of given numbers. Standard deviation is used to measure deviation of data from its mean. In other words, standard deviation is the measure of how spread-out numbers are. Its symbol is sigma (σ).

In the case of normal distributions of numbers, a larger standard deviation means that the given numbers are generally far from the mean, while a smaller standard deviation value means the numbers are clustered closer to the mean. Check this page to learn about Standard Deviation

To understand this java program, you should have understanding of the following Java programming concepts:


Java Program to Calculate Standard Deviation

public class StandardDeviation {
  public static double findSD(double data[]) {
    double sum = 0.0, sd = 0.0;
    int arrayLength = data.length;
    // Calculate sum
    for(double d : data) {
      sum += d;
    }
    // Calculate mean
    double mean = sum/arrayLength;
    // Calculate standard deviation
    for(double d: data) {
      sd += Math.pow(d - mean, 2);
    }
    return Math.sqrt(sd/arrayLength);
  }

  public static void main(String[] args) {
    double[] data = { 10, 25, 35, 55, 72, 75, 98, 105};
    double standardDeviation = findSD(data);
    System.out.format("Standard Deviation : %.4f",
            standardDeviation);
  }
}
Output
Standard Deviation : 32.0349

In above java program, we calculated standard deviation of numbers [10, 25, 35, 55, 72, 75, 98, 105] as follows:

Count, N:	8
Sum, Σx:	475
Mean, μ:	59.375
Variance, σ2: 	1026.234375
Standard Deviation = sqrt(Variable) = 32.0348