We have to understand few concepts such as Mean and Variance before understand the Standard Deviation. To understand these concepts better, we are taking some example data here.

For example, We have 5 items and their Price values are 10, 25, 30, 67, 92. Let us calculate the Mean, Variance and Standard Deviation in C programming.

### Mean in C

Mean can also called as Average and we can calculate using the formula:

Mean = Sum of each individual/total number of items

Mean = (10 + 25 + 30 + 67+ 92) / 5

Mean = 224 / 5 = 44.8

### Variance in C

Before Calculating the Variance in C, we have to find the difference between the original value and the Mean because

Difference = ((OriginalValue – Mean)² + (OriginalValue – Mean)² +…. )/Total number of items

Difference = (10 – 48) + (25 – 48) + (30 – 48) + (67 – 48) + (92 – 48)

Difference = (- 38) + (- 23) + (- 18) + (19) + (44)

We can calculate the Variance using the formula:

Variance = ( (- 38)² + (- 23)² + (- 18)² + (19)² + (44)² ) / 5

Variance = (1444 + 529 + 324 +361 + 1936) / 5

Variance = 4594 / 5

Variance = 918.8

### Standard Deviation in C

The Square root of Variance is called as Standard Deviation.

Standard Deviation = √918.8

Standard Deviation = 30.31

In this article, we will show you, How to write a C Program to Calculate Standard Deviation, Mean and variance with an example.

## C Program to Calculate Standard Deviation, Mean and Variance

This C program calculate the Mean, Variance and Standard Deviation of the array of given numbers.

/* C Program to Calculate Standard Deviation, Mean and Variance */ #include <stdio.h> #include <math.h> void main() { float Price[50]; int i, Number; float Mean, Variance, SD, Sum=0, Differ, Varsum=0; printf("\nPlease Enter the N Value\n"); scanf("%d", &Number); printf("\nPlease Enter %d real numbers\n",Number); for(i=0; i<Number; i++) { scanf("%f", &Price[i]); } for(i=0; i<Number; i++) { Sum = Sum + Price[i]; } Mean = Sum /(float)Number; for(i=0; i<Number; i++) { Differ = Price[i] - Mean; Varsum = Varsum + pow(Differ,2); } Variance = Varsum / (float)Number; SD = sqrt(Variance); printf("Mean = %.2f\n", Mean); printf("Varience = %.2f\n", Variance); printf("Standard deviation = %.2f\n", SD); }

**OUTPUT**

**ANALYSIS**

Within this C Program to Calculate Standard Deviation, First Printf statement

printf("\n Please Enter the N Value \n");

Next statement ask the user to enter the N value (Number of items inside the Price Array)

scanf(“%d”, &Number) statement will assign the user input value to variable Number

First For loop

for(i=0; i<Number; i++) { scanf("%f", &Price[i]); }

(i<Number) condition helps the compiler not to exceed more than N value (Since array starts with 0 and end with N-1 we mentioned i< Number)

scanf("%f", &Price[i]) )

The scanf statement will assign the user input values to Price array. So the first value will be assigned to Price[0], second at Price[1] etc.

Second For loop

for(i=0; i<Number; i++) { Sum = Sum + Price[i]; }

Sum = Sum + Price[i]; statement will calculate the sum of each individual element in the Price Array

Initially we assigned Sum = 0 so, the first iteration will be

Sum = 0 + Price[0] = 0+ 10 = 10

Sum = 10 + Price[1] = 10 + 20 = 30

Sum = 30 + Price[2] = 30 + 30 = 60

Sum = 60 + Price[3] = 60 + 40 = 100

Sum = 100 + Price[4] = 100 + 50 = 150

Next line, we are calculating the Mean or average in C

Mean = Sum /(float)Number;

Mean = 150 / 5 = 30

Since the Number is an integer value and Mean is float value, we have to type cast the Number value to float (i.e (float)Number). If you forgot to Type cast then it will give wrong values.

Third For loop

for(i=0; i < Number; i++) { Differ = Price[i] - Mean; Varsum = Varsum + pow(Differ, 2); }

Within the for loop, pow() is the math function which will calculate the power of Differ by 2 times (Equal to Differ2).

Instead of writing in two lines we can write the above code in 1 line also:

Varsum = Varsum + pow( (Price[i] - Mean), 2); Varsum At First Iteration Varsum = 0 + pow( (Price[0] - 30), 2) = 0 + pow( (10 - 30), 2) = 0 + (20 * 20) = 400 Varsum At Second Iteration Varsum = 400 + pow( (Price[1] - 30), 2) = 400 + pow( (20 - 30), 2) = 400 + (10 * 10) = 500 Varsum At Third Iteration Varsum = 500 + pow( (Price[2] - 30), 2) = 500 + pow( (30 - 30), 2) = 500 + (0 * 0) = 500 Varsum At Fourth Iteration Varsum = 500 + pow( (Price[3] - 30), 2) = 500 + pow( (40 - 30), 2) = 500 + (10 * 10) = 600 Varsum At Fifth Iteration Varsum = 600 + pow( (Price[4] - 30), 2) = 600 + pow( (50 - 30), 2) = 600 + (20 * 20) = 1000

Next line we are calculating the Variance in C programming

Variance = Varsum / (float)Number; Variance = 1000 / 5 = 200

Now, its time to calculate the Standard Deviation in c programming

SD = sqrt(Variance); SD = sqrt (200) = 14.142

sqrt() is a math function which is used to calculate the Square Root of any given number

## C Program to Calculate Standard Deviation using Functions

This C program calculate the Mean, Variance and Standard Deviation of the given numbers in an array using Functions.

We have already seen in the above example, there are a lot of for loops and lot of calculation. It is often confusing to put it all together so, in this program we separated the calculation using Function concept.

#include <stdio.h> #include <math.h> float StandardDeviation (float Price[],int Number); void main() { float Price[50], SD; int i, Number; printf("\nPlease Enter the N Value\n"); scanf("%d", &Number); printf("\nPlease Enter %d real numbers\n",Number); for(i=0; i<Number; i++) { scanf("%f", &Price[i]); } //Calling the Function StandardDeviation SD = StandardDeviation (Price, Number); printf("Standard deviation = %.2f\n", SD); } float StandardDeviation (float Price[],int Number) { float Mean, Variance, SD, Sum=0, Varsum=0; int i; for(i=0; i<Number; i++) { Sum = Sum + Price[i]; } Mean = Sum /(float)Number; for(i=0; i<Number; i++) { Varsum = Varsum + pow((Price[i] - Mean),2); } Variance = Varsum / (float)Number; SD = sqrt(Variance); printf("Mean = %.2f\n", Mean); printf("Varience = %.2f\n", Variance); return SD; }

**OUTPUT**

**ANALYSIS:**

Within this C Program to Calculate Standard Deviation, When the compiler reaches the statement

SD = StandardDeviation (Price, Number);

Then it will jump to function

float StandardDeviation (float Price[ ], int Number)

For better understanding, we used the same arguments in the function declaration and Function Calling. In real time, names may change but the data types should be the same.

float StandardDeviation (float Price[ ],int Number)

Within the above function, we calculated the Mean, Variance and Standard Deviation of the array of N numbers. Logic inside the function is same as we explained in the first example So To understand Please refer First Example Analysis section

To understand the Functions, it’s better to revise Function article in our Blog: Functions.

Thank you for visiting Our Blog