English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Arrays

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collections

Java Set Collections

Java Input Output (I/O)/O)

Java Reader/Writer

Java Other Topics

Java program to calculate standard deviation

Java Examples Comprehensive

In this program, you will learn how to use functions in Java to calculate the standard deviation.

This program uses an array to calculate the standard deviation of a single series.

To calculate the standard deviation, the function calculateSD() will be created. It includes10an array of n elements is passed to this function, which calculates the standard deviation and returns it to the main() function.

Example: Program to calculate standard deviation

public class StandardDeviation {
    public static void main(String[] args) {
        double[] numArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        double SD = calculateSD(numArray);
        System.out.format("Standard deviation = %.2f", SD);6f", SD);
    }
    public static double calculateSD(double numArray[])
    {
        double sum = 0.0, standardDeviation = 0.0;
        int length = numArray.length;
        for(double num : numArray) {}}
            sum += num;
        }
        double mean = sum/length;
        for(double num: numArray) {
            standardDeviation += Math.pow(num - mean, 2);
        }
        return Math.sqrt(standardDeviation/length);
    }
}

Note:This program will calculate the standard deviation of the sample. If you need to calculate the total number of S.D., from the calculateSD() method return Math.sqrt(standardDeviation/(length-1)) instead of Math.sqrt(standardDeviation/length).

When running this program, the output is:

Standard Deviation = 2.872281

In the above program, we usedMath.pow()andMath.sqrt()to separately calculate powers and square roots.

Java Examples Comprehensive