C Program to Calculate Standard Deviation
The Following C program to Calculate Standard Deviation, Ready to execute code with clean output, in easy way with simple steps.
In statistics, standard deviation is used to measure deviation of data from its mean. The formula for calculating standard deviation of n items is
The algorithm for calculating the standard deviation is as follows:
- Read n items.
- Calculate sum and mean of the items.
- Calculate variance.
- Calculate standard deviation.
Complete program with sample output is shown in below code,
To understand this example, you should have also knowledge of C programming Arrays.
Example : Calculate Standard Deviation
#include <math.h> #define MAXSIZE 100 main( ) { int i,n; float value [MAXSIZE], deviation, sum,sumsqr,mean,variance,stddeviation; sum = sumsqr = n = 0 ; printf("Input values: input -1 to end \n"); for (i=1; i< MAXSIZE ; i++) { scanf("%f", &value[i]); if (value[i] == -1) break; sum += value[i]; n += 1; } mean = sum/(float)n; for (i = 1 ; i<= n; i++) { deviation = value[i] - mean; sumsqr += deviation * deviation; } variance = sumsqr/(float)n ; stddeviation = sqrt(variance) ; printf("\nNumber of items : %d\n",n); printf("Mean : %f\n", mean); printf("Standard deviation : %f\n", stddeviation); }
Output display
Input values: input -1 to end 65 9 27 78 12 20 33 49 -1 Number of items : 8 Mean : 36.625000 Standard deviation : 23.510303
Let me know if you find any difficulty in understanding this C Program with example and I would be glad to explain it further.