Algorithm


  1. Input:

    • The program starts by taking user input for the number of elements in the dataset (n) and then prompts the user to enter each element.
  2. Calculate the Mean:

    • The calculateMean method is called to find the mean of the entered data set.
  3. Calculate Squared Differences:

    • The calculateSquaredDifferences method is called to find the squared differences between each element and the mean.
  4. Calculate the Variance:

    • The calculateVariance method is called to find the variance, which is the mean of the squared differences.
  5. Calculate the Standard Deviation:

    • The standard deviation is then calculated by taking the square root of the variance obtained in step 4.
  6. Output:

    • The final standard deviation is printed to the console.

Now, let's briefly discuss the implementation of the three methods:

  • calculateMean Method:

    • It calculates the mean by summing up all the elements in the data set and dividing by the total number of elements.
  • calculateSquaredDifferences Method:

    • It calculates the squared differences between each element and the mean. The results are stored in an array.
  • calculateVariance Method:

    • It calculates the variance by finding the mean of the squared differences obtained in the previous step.

 

Code Examples

#1 Code Example- Program to Calculate Standard Deviation

Code - Java Programming

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 = %.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);
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Standard Deviation = 2.872281
Advertisements

Demonstration


Java Programing Example to Calculate Standard Deviation-DevsEnv