C Program to find the mean, variance and standard deviation

May 1, 2022, Learn eTutorial

What is Mean?

Mean: it is the average of all elements in a set of values. Mean add the values in a set and divide the sum with the number of elements. For example, if a set has 3 numbers [1,2,3] then the mean is calculated using 1+2+3 = 6/3 =2.

What is a Variance?

Variance: it is the average of the squared difference between the mean and the values of the data set. It is called the spread of data within a sample of data.

What is Standard deviation?

Standard deviation: after we found the variance, we can calculate the standard deviation by taking the square root of the variance.

In this C program, we take the values and save that in an array. The mean calculation is done by taking the average of the sum by using a for loop. After that, we need to calculate variance using the formula.

 v_sum = v_sum + pow((nums[i] - avrg),2); 
 var = v_sum / (float) n; 

After calculating the variance, it is easy to find the standard deviation. for that, we use the sd = sqrt(var) formula. Finally, we have to display the results using printf.


STEP 1: Include the Header files into the C program to use the built-in functions.

STEP 2: Define and initialize the Array and the variables.

STEP 3: Accept the value for the number of terms from the user using printf and scanf functions.

STEP 4: Accept the numbers from the user and add those numbers into an Array using for loop and scanf.

STEP 5: Open a for loop from i=0 to the number of terms n to calculate the mean. Add each element and save it in sum, and divide sum with the number of terms.

STEP 6: Open another for loop from i=0 to n to find the variance. In each iteration of loop calculate,

 v_sum = v_sum + pow((nums[i] - mean),2) , and after that use the formula var = v_sum / (float) n

STEP 8: Calculate the Standard deviation sd by taking the square root of var.

STEP 10: print the mean, sd, and var using printf.

C Source Code

                                          #include <stdio.h>
#include <math.h>
#define MAXSIZE 10

void main()

        float nums[MAXSIZE];
        int  i, n;
        float mean, var, sd, sum=0, v_sum=0;   /* declares the variables*/
        printf("Enter the number of elements\n");   /* accepts values from user */
        scanf("%d", &n);
        printf("Enter %d real numbers\n",n);
           for (i = 0; i < n; i++)
                scanf("%f", & nums[i]);
            for(i=0; i<n; i++)   /* Compute the sum of all elements */
                  sum = sum + nums[i]; 
           mean = sum /(float) n;  /* Calculating the mean using the equation */
           for(i=0; i<n; i++)  /* Compute  variance  and standard deviation */
                  v_sum= v_sum + pow((nums[i] - mean), 2);                  
           var = v_sum / (float) n;   /* calculate the variance using general equation */                   
           sd = sqrt(var);  /* calculating standard deviation */
           /* prints the output mean, standard deviation, and variance */
           printf("Mean of all elements  = %.2f\n", mean);
           printf("The variance of all elements = %.2f\n", var);
           printf("Standard deviation SD = %.2f\n", sd);  


Enter the number of elements
Enter 6 real numbers
Mean of all elements  = 30.17
The variance of all elements = 215.47
Standard deviation SD = 14.68