## Free Data Structures and Algorithms Course

### How to Calculate Standard Deviation through a Java program ?.

Program to Calculate Standard Deviation in Java

```package com.hubberspot.example;

public class StandardDeviation {

public static void main(String[] args) {

double[] numbers = new double;

// Take the 10 numbers in array for which we
// want to calculate the standard deviation
numbers = 23;
numbers = 92;
numbers = 46;
numbers = 55;
numbers = 63;
numbers = 94;
numbers = 77;
numbers = 38;
numbers = 84;
numbers = 26;

System.out.println("1. Get the mean of numbers. The mean is : ");

double sum = 0;

// Taking the average to numbers
for(int i =0; i< numbers.length; i++) {
sum = sum + numbers[i];
}

double mean = sum/numbers.length;

System.out.println(mean);

System.out.println("\n2. Get deviation of mean from each number : ");

double[] deviations = new double;

// Taking the deviation of mean from each numbers
for(int i = 0; i < deviations.length; i++) {
deviations[i] = numbers[i] - mean ;
System.out.printf("%2.2f",deviations[i]);
System.out.print(" ");
}

System.out.println();
System.out.println("\n3. Get squares of deviations : ");
double[] squares = new double;

// getting the squares of deviations
for(int i =0; i< squares.length; i++) {
squares[i] = deviations[i] * deviations[i];
System.out.printf("%4.2f",squares[i]);
System.out.print(" ");
}

System.out.println();
System.out.println("\n4. Get addition of squares : ");

sum = 0;

for(int i =0; i< squares.length; i++) {
sum = sum + squares[i];
}

System.out.println(sum);

// dividing the numbers by one less than total numbers
System.out.println("\n5. Divide addition of squares by total (numbers) - 1 : ");
double result = sum / (numbers.length - 1);

System.out.printf("%4.2f",result);

double standardDeviation = Math.sqrt(result);

// Taking square root of result gives the
// standard deviation
System.out.println("\n\n6. Take the square root of result which gives");
System.out.println("the Standard Deviation of the ten numbers : ");
System.out.printf("%4.2f",standardDeviation);

}

}

```
Output of the program :

A video tutorial to demonstrate how to calculate Standard Deviation through a Java program.