Java Program to Calculate Standard Deviation - GeeksforGeeks

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 - ų)2 / N 
              where, 
                Xi = each element of the array
                ų = 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
First, create a class called calculateSD2 Then create the main method and then in the main method create an object of the above class and call it using the object.Then declare an array in this class with the values given in the above example.Now, in order to iterate through the array, we need to find the size of the array.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.Then, again use the for-loop and iterate through the array in order to calculate the sum of the elements of the array.After that, the mean will be calculated by mean = sum / n, where n is the number of elements in the array.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.After that, the value will be returned by that class and then print.
First, create a class called calculateSD2 
Then create the main method and then in the main method create an object of the above class and call it using the object.
Then declare an array in this class with the values given in the above example.
Now, in order to iterate through the array, we need to find the size of the array.
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.
Then, again use the for-loop and iterate through the array in order to calculate the sum of the elements of the array.
After that, the mean will be calculated by mean = sum / n, where n is the number of elements in the array.
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.
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);    }}
Elements are:
12
32
11
55
10
23
14
30
Standard Deviation = 14.438988
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Hashtable in Java
Constructors in Java
Different ways of Reading a text file in Java
Comparator Interface in Java with Examples
HashMap containsKey() Method in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?