C Programming Code Examples C > Mathematics Code Examples C Program to Calculate the Mean, Variance & Standard Deviation C Program to Calculate the Mean, Variance & Standard Deviation This C Program calculates the mean, variance & standard deviation. The formula which is used in this program are mean = average of the numbers. variance = (summation( ( Xj - average of numbers) * ( Xj - average of numbers)) ) / Total no of elements. where j = 1 to N here N is the total no of elements. Standard deviation = Squareroot of the variance. #include <stdio.h> #include <math.h> #define maxsize 10 void main() { float x[maxsize]; int j, n; float average, variance, std_deviation, sum = 0, sum1 = 0; printf("Enter the value of N \n"); scanf("%d", &n); printf("Enter %d real numbers \n", n); for (j = 0; j < n; j++) { scanf("%f", &x[j]); } /* Compute the sum of all elements */ for (j = 0; j < n; j++) { sum = sum + x[j]; } average = sum / (float)n; /* Compute variance and standard deviation */ for (j = 0; j < n; j++) { sum1 = sum1 + pow((x[j] - average), 2); } variance = sum1 / (float)n; std_deviation = sqrt(variance); printf("Average of all elements = %.2f\n", average); printf("variance of all elements = %.2f\n", variance); printf("Standard deviation = %.2f\n", std_deviation); }