calculate standard deviation (#12963)

Co-authored-by: tienvn4 <tienvn4@ghtk.co>
This commit is contained in:
vunamtien 2022-11-01 22:38:06 +07:00 committed by GitHub
parent 1f1d49bcb1
commit 5c281eb016
1 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package com.baeldung.math.standarddeviation;
import java.util.Arrays;
public class StandardDeviation {
public static double calculateStandardDeviation(double[] array) {
// get the sum of array
double sum = 0.0;
for (double i : array) {
sum += i;
}
// get the mean of array
int length = array.length;
double mean = sum / length;
// calculate the standard deviation
double standardDeviation = 0.0;
for (double num : array) {
standardDeviation += Math.pow(num - mean, 2);
}
return Math.sqrt(standardDeviation / length);
}
public static void main(String[] args) {
double[] array = {25, 5, 45, 68, 61, 46, 24, 95};
System.out.println("List of elements: " + Arrays.toString(array));
double standardDeviation = calculateStandardDeviation(array);
System.out.format("Standard Deviation = %.6f", standardDeviation);
}
}