How to Calculate Standard Deviation by Java Program

Java Program to Calculate Standard Deviation.

Java Array and Methods.

  • An object of a class that is dynamically produced in Java is an array. Java array implements the Serializable and Cloneable interfaces and derives from the Object class. The first element of an array is stored at index 0, the second member is kept at index 1, and so forth. Arrays in Java are index-based.
  • A method in Java is a section of code that completes a certain task. Code can be contained in methods, which makes it more reusable and understandable.Java methods make it simpler to build and maintain complicated programmes by giving coders a means to organise and reuse code.

To know more about Java Program to calculate Standard Deviation read the complete article.

Steps to Calculate Standard Deviation by Java Program

  • Here are the steps to calculate the standard deviation in Java:

    • First, you need to find the mean (average) of the data set.

    • Then, subtract the mean from each data point to find the deviations.

    • Square each deviation to remove the negative sign.

    • Find the sum of all the squared deviations.

    • Divide the sum by the number of data points minus one (n-1).

  • Take the square root of the result to find the standard deviation.

Let’s look at a Java Program to calculate Standard Deviation to perform certain operations.

Example 1: Java Program to Calculate standard Deviation.

Run
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    double[] input = {10, 20, 35, 40, 45};
    double sum = 0, mean;
    int n = input.length;

    for (int i = 0; i < n; i++) {
      sum += input[i];
    }
    mean = sum / n;
    System.out.println("Mean: " + mean);

    sum = 0;
    for (int i = 0; i < n; i++) {
      sum += Math.pow(input[i] - mean, 2);
    }
    mean = sum / (n - 1);
    double deviation = Math.sqrt(mean);
    System.out.println("Standard deviation: " + deviation);
  }
}

Output

Mean: 15.0
Standard deviation: 7.0710678118654755

Example 2 : Java Program to Calculate Standard Deviation 

Run
class Main 
{
   public static void main(String args[]) 
    { 
	double[] input={5,10,15,20,25};
	double n=5,sum=0,mean;
	for(int i=0;i<n;i++) 
	{
		sum=sum+input[i];
	}
	mean=sum/n;
       	System.out.println("Mean :"+mean);
	sum=0;  
	for(int i=0;i<n;i++) 
	{
		sum+=Math.pow((input[i]-mean),2);
	
	}
	mean=sum/(n-1);
	double deviation=Math.sqrt(mean);
	System.out.println("standard deviation :"+deviation);

   }
}

Output

Mean :15.0
standard deviation :7.905694150420948

Example 3: Java Program to Calculate Standard Deviation: 

Run
import java.util.Arrays;

public class Main {
  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);
  }

  public static void main(String[] args) {
    double[] numArray = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
    System.out.format("Standard Deviation = %.6f", calculateSD(numArray));
  }
}

Output

Standard Deviation = 2.581989

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription