Java Program to Calculate Standard Deviation

• Difficulty Level : Medium
• Last Updated : 27 Nov, 2020

The standard deviation is the measure of how spread out numbers are. Its symbol is sigma( σ ). It is the square root of variance. The task is to calculate the standard deviation of some numbers.

Consider an example that consists of 6 numbers and then to calculate the standard deviation, first we need to calculate the sum of 6 numbers, and then the mean will be calculated. Then the standard deviation will be calculated using the standard deviation formula.

Standard deviation = square root of ∑(Xi - ų)2 / N
where,
Xi = each element of the array
ų = mean of the elements of the array
N = Number of elements
∑ = Sum of the each element
Input : [12, 32, 11, 55, 10, 23, 14, 30]
Output : 14.438988

Input : [10, 12, 22, 34, 21]
Output : 8.541663

Approach: Using Mathematical Formula

1. First, create a class called calculateSD2
2. Then create the main method and then in the main method create an object of the above class and call it using the object.
3. Then declare an array in this class with the values given in the above example.
4. Now, in order to iterate through the array, we need to find the size of the array.
5. Now, use the for-loop and iterate through this array and increment it by 1 as we need to print all the elements of the array.
6. Then, again use the for-loop and iterate through the array in order to calculate the sum of the elements of the array.
7. After that, the mean will be calculated by mean = sum / n, where n is the number of elements in the array.
8. Now, the standard deviation will be calculated with the help of mean, which is done by iterating through for-loop again and with the help of Mathematical predefined methods like Math.pow and Math.sqrt.
9. After that, the value will be returned by that class and then print.

Java

 // Java program to calculate the standard deviation  class calculateSD2 {    double sum = 0.0;    double standardDeviation = 0.0;    double mean = 0.0;    double res = 0.0;    double sq = 0.0;      double SD()    {        int[] arr = { 12, 32, 11, 55, 10, 23, 14, 30 };        int n = arr.length;                System.out.println("Elements are:");        for (int i = 0; i < n; i++) {            System.out.println(arr[i]);        }                for (int i = 0; i < n; i++) {            sum = sum + arr[i];        }                mean = sum / (n);          for (int i = 0; i < n; i++) {                        standardDeviation                = standardDeviation + Math.pow((arr[i] - mean), 2);                    }                sq = standardDeviation / n;        res = Math.sqrt(sq);        return res;    }}  public class Standard {    public static void main(String[] args)    {          calculateSD2 calsd = new calculateSD2();        double res = calsd.SD();          System.out.format("Standard Deviation = %.6f", res);    }}
Output
Elements are:
12
32
11
55
10
23
14
30
Standard Deviation = 14.438988

My Personal Notes arrow_drop_up