C Program to find the mean, variance and standard deviation

Here in this C program, we need to find out Mean, Variance, and Standard deviation; we need to know what is meant by Mean, Standard deviation, and Variance.

What is Mean?

Mean: it is the average of all elements in a set of values. Means 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.

Explain the term 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 by taking the average of the sum by using a 'for loop' after that, we need to calculate variance using the formula.


"sum1 = sum1 + pow((x[i] - avrg),2)" 
 var = sum1 / (float) n; 

After calculating the variance, it is easy to find the standard deviation. Estimate the standard deviation of the set; we use the sd = sqrt(var) formula in this c program. Finally, we have to display the mean, variance, and standard deviation using printf in the c language.


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 used in the program.

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 zero to the number of terms to calculate the Mean.

STEP 6: Calculate Mean by finding the Sum and divide that with the number of terms.

STEP 7: Open another For loop from zero to 'n' to find the Variance.

STEP 8: Calculate the Variance using the formula sum1 = sum1 + pow((x[i] - avrg),2 and apply the sum1 in main formula var = sum1 / (float) n.

STEP 9: Calculate the Standard deviation by taking the square root of Variance.

STEP 10: print the Mean, Standard deviation, and Variance using printf built-in functions in C.

C Source Code

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

void main()

        float x[MAXSIZE];
        int  i, n;
        float avrg, var, SD, sum=0, sum1=0;   /* declares the variables avrg, Sd, sum as float */
        printf("Enter the value of N\n");                  /* accepts values from user */
        scanf("%d", &n);
        printf("Enter %d real numbers\n",n);
           for (i = 0; i < n; i++)
                scanf("%f", &x[i]);
            for(i=0; i<n; i++)                                        /* Compute the sum of all elements */
                  sum = sum + x[i];                                     /* calculating the mean using the equation */
       avrg = sum /(float) n;
       for(i=0; i<n; i++)                                 /* Compute  variance  and standard deviation */
                  sum1 = sum1 + pow((x[i] - avrg), 2);    /* calculate the variance using general equation */
      var = sum1 / (float) n;                     
      SD = sqrt(var);                                                      /* calculating standard deviation */
      printf("Average of all elements  = %.2f\n", avrg);
      printf("Varience of all elements = %.2f\n", var);
      printf("Standard deviation SD = %.2f\n", SD);               /* prints the output mean, standard deviation, and variance */


Enter the value of N


Enter 6 real numbers







Average of all elements  = 30.17

Varience of all elements = 215.47

Standard deviation SD = 14.68