# Java Program to Calculate Standard Deviation

In this article, we will understand how to calculate standard deviation. The standard deviation is the measure of how spread-out numbers are. Its symbol is sigma( σ ). It is the square root of variance.

Standard deviation is computed using the formula square root of ∑(Xi - ų)2 / N where Xi is the element of the array, ų is mean of the elements of the array, N is the number of elements, ∑ is the sum of the each element.

Below is a demonstration of the same −

Suppose our input is

Input Array : [ 35.0, 48.0, 60.0, 71.0, 80.0, 95.0, 130.0 ]

The desired output would be

Standard Deviation: 29.313227

## Algorithm

Step 1 - START
Step 2 - Declare a double array namely input_array, two doube values namely sum and standard_deviation.
Step 3 - Read the required values from the user/ define the values.
Step 4 - Compute ∑(Xi - ų)2 / N and store the value in result variable.
Step 5 - Display the result
Step 6 - Stop

## Example 1

Here, the input is being entered by the user based on a prompt.

public class StandardDeviation {
public static void main(String[] args) {
double[] input_array = { 35, 48, 60, 71, 80, 95, 130};
System.out.println("The elements of the array is defined as");
for (double i : input_array) {
System.out.print(i +" ");
}
double sum = 0.0, standard_deviation = 0.0;
int array_length = input_array.length;
for(double temp : input_array) {
sum += temp;
}
double mean = sum/array_length;
for(double temp: input_array) {
standard_deviation += Math.pow(temp - mean, 2);
}
double result = Math.sqrt(standard_deviation/array_length);
System.out.format("\n\nThe Standard Deviation is: %.6f", result);
}
}

## Output

The elements of the array is defined as
35.0 48.0 60.0 71.0 80.0 95.0 130.0

The Standard Deviation is: 29.313227

## Example 2

Here, we have defined a function to compute standard deviation.

public class StandardDeviation {
public static void main(String[] args) {
double[] input_array = { 35, 48, 60, 71, 80, 95, 130};
System.out.println("The elements of the array is defined as");
for (double i : input_array) {
System.out.print(i +" ");
}
double standard_deviation = calculateSD(input_array);
System.out.format("\n\nThe Standard Deviation is: %.6f", standard_deviation);
}
public static double calculateSD(double input_array[]) {
double sum = 0.0, standard_deviation = 0.0;
int array_length = input_array.length;
for(double temp : input_array) {
sum += temp;
}
double mean = sum/array_length;
for(double temp: input_array) {
standard_deviation += Math.pow(temp - mean, 2);
}
return Math.sqrt(standard_deviation/array_length);
}
}

## Output

The elements of the array is defined as
35.0 48.0 60.0 71.0 80.0 95.0 130.0

The Standard Deviation is: 29.313227