1. Overview
In this tutorial, you'll learn how to calculate the standard deviation in java using for loop and its formula.
2. Example to calculate standard deviation
Let us write a simple java program to find the standard deviation for an individual series of numbers.
First create a method with name calculateStandardDeviation(). Create an array with integer values and. pass this array to the calculateStandardDeviation() method. This method has the core logic to get the standard deviation and. returns to main method.
package com.javaprogramto.programs.arrays.calculation; public class StandardDeviation { public static void main(String[] args) { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; double standardDeviation = calculateStandardDeviation(array); System.out.format("Standard deviation : %.6f", standardDeviation); } private static double calculateStandardDeviation(int[] array) { // finding the sum of array values double sum = 0.0; for (int i = 0; i < array.length; i++) { sum += array[i]; } // getting the mean of array. double mean = sum / array.length; // calculating the standard deviation double standardDeviation = 0.0; for (int i = 0; i < array.length; i++) { standardDeviation += Math.pow(array[i] - mean, 2); } return Math.sqrt(standardDeviation/array.length); } }
Output:
Standard deviation : 2.581989
3. Conclusion
In this article, you've seen how to find the standard deviation in java.
As usual, the shown example is over GitHub.
No comments:
Post a Comment
Please do not add any spam links in the comments section.