Standard deviation java code – Java Program to Calculate Standard Deviation

Standard deviation java code: In the previous article, we have seen Java Program to Find the Elements from the Array which are Smaller than the Average of Array

In this article we are going to see how to Calculate Standard Deviation using java programming language.

Java Program to Calculate Standard Deviation

Java calculate standard deviation: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to calculate standard deviation using java programming language.

Method-1: Java Program to Calculate Standard Deviation By Static Initialization of Array Elements

Approach:

  1. Find the array sum.
  2. Calculate the mean.
  3. Calculate squared difference from mean.
  4. Return the square root of the squared difference.

 Program:

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        double[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        // call the method
        double std = calc_std(arr);
        // print the result
        System.out.format("Standard Deviation = %.6f", std);
    }

    public static double calc_std(double arr[]) 
    {
        double sum = 0.0, std = 0.0;
        int length = arr.length;

        // find array sum
        for (double num : arr) 
        {
            sum += num;
        }
        // calculate the mean
        double mean = sum / length;

        // calculate squared difference from mean
        for (double num : arr) 
        {
            std += Math.pow(num - mean, 2);
        }
        // return the square root of the squared difference(standard deviation)
        return Math.sqrt(std / length);
    }
}

Output:

Standard Deviation = 2.872281

Method-2: Java Program to Calculate Standard Deviation By Dynamic Initialization of Array Elements

Approach:

  1. Create scanner class object.
  2. Ask use length of the array.
  3. Initialize the array with given size.
  4. Ask the user for array elements.
  5. Find the array sum.
  6. Calculate the mean.
  7. Calculate squared difference from mean.
  8. Return the square root of the squared difference.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        double[] arr = new double[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextDouble();
        }
        // call the method
        double std = calc_std(arr);
        // print the result
        System.out.format("Standard Deviation = %.6f", std);
    }

    public static double calc_std(double arr[]) 
    {
        double sum = 0.0, std = 0.0;
        int length = arr.length;

        // find array sum
        for (double num : arr) 
        {
            sum += num;
        }
        // calculate the mean
        double mean = sum / length;

        // calculate squared difference from mean
        for (double num : arr) 
        {
            std += Math.pow(num - mean, 2);
        }
        // return the square root of the squared difference(standard deviation)
        return Math.sqrt(std / length);
    }
}

Output:

Enter the size of array: 5
Enter array elements: 1 2 3 4 5
Standard Deviation = 1.414214

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: