Latest :

Java Program to Calculate Variance & Standard Deviation | Java Programs

To solve any given problem, we first have to understand the problem statements thoroughly and see if any constraints are given in the problem. Then, we have to determine the requirements or inputs needed to solve the problem. Then, we decide our expected output for the given problem and finally, think of the logic to be used to arrive at our output.

Our problem statement here is, to find the variance and then standard deviation for a given set of numbers. For this, our input will be an array of integers. We’ll have two outputs here. One is the variance while other is the standard deviation.

Firstly, we need to gather all the required inputs. It is always preferable to read input at runtime than give it in the code itself.

This is because, if directly given in code, we will have to make changes every time we would like to try with a different set of input values. For this, one method is by making use of the Scanner class in Java to read any primitive datatype input at runtime by creating an object for it in our class.

Scanner sc=new Scanner(System.in);

So first we’ll read the size of the array or number of data values for which variance and standard deviation has to be calculated using nextInt() method of Scanner class. Following this, we will create an array of that particular size and read all the data values as follows:

 

As we read the data values, we’ll directly keep adding the value to another variable (sum) which is used to store the sum total of all the data values. As we complete reading all the data values, we’ll also have the sum of all these data values simultaneously.

For calculation of variance, we first need to calculate the mean of these data values. The mean can be defined as the sum total of all the elements divided by the total number of elements. So the formula for mean is,

m=sum/n;

For calculation of variance, we need a variable to store the sum of the square of the difference between individual elements and the mean.

Since our use of ‘sum’ variable for the calculation of mean is already done, we’ll first initialize it to zero and make use of the same variable. So first, well calculate the difference of each data value to the mean, then square it, and then this will keep adding to the sum variable which stores the cumulative sum of all.

sum=0;

for(int i=0;i<n;i++) {

sum+=(a[i]-m)*(a[i]-m);

}

Now, variance is equal to this cumulative sum divided by total number of data values as follows:

v=sum/n;

After calculating variance, standard deviation is very simple. Standard deviation is nothing but, the square-root of variance. In Java, to find the square-root, we can make use of sqrt() method of the Math package as follows:

sd=Math.sqrt(v);

After getting all our outputs, all we have to do is print or display it in our output console screen.

System.out.println(“mean”+m);

System.out.println(“variance”+v);

System.out.println(“standard deviation”+sd);

Output:

x

Check Also

Java Program to Add Two Matrices – 4 Ways | Programs

Java program to add two matrices – The following Java Code will let you know ...