- 0
Notifications Mark All Read
No New notification
- Login
- Get Prime
How to Calculate Standard Deviation by Java Program
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.
CODE OPTIMIZATION: Java arrays are simple to read and edit because they are kept in memory as continuous chunks. By lowering the amount of memory operations needed to access elements and the time required to complete these operations, this can aid in code optimization.
Random Access: Java arrays allow for random access of elements, meaning that any element can be accessed directly by its index without having to traverse the entire array. This can help to optimize code by reducing the amount of time needed to find an element in the array, especially for large arrays.
Let’s look at a Java Program to calculate Standard Deviation to perform certain operations.
Example 1: Java Program to Calculate standard Deviation.
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
- This code is a Java program that calculates the mean and standard deviation of an array of 5 values (5, 10, 15, 20, 25).
- The mean is calculated by adding all the elements in the array and dividing by the number of elements. The standard deviation is calculated by finding the variance, which is the average of the squared differences between each element and the mean, and then taking the square root of the variance.
- The results are printed on the console in the format "Mean: [mean value]" and "Standard deviation: [standard deviation value]".
Example 2 : Java Program to Calculate Standard Deviation
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:
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
Login/Signup to comment