C Program To Calculate Standard Deviation

In this post, we will learn how to calculate standard deviation using C Programming language.

C Program To Calculate Standard Deviation

A Standard Deviation is a statistic that measures how much the members of a group differ from the mean value of the group.

To calculate the standard deviation, we have to follow these steps:

  1. Find out the Mean
  2. Then for each element, subtract the Mean and Square the result
  3. Then work out the Mean of those squared differences 
  4. Take the square root of that 

We will be using the exact same procedure in our program to compute the standard deviation.

So, without further ado, let’s begin this tutorial.

C Program To Calculate Standard Deviation

// C Program To Calculate Standard Deviation
#include <stdio.h>
#include <math.h>

int main(){
    int i, n;
    float num[25];
    float sum = 0.0, mean, SD;
    
    // Asking for input
    printf("Enter total number of elements: ");
    scanf("%d", &n);
    printf("Enter the value of elements: \n");
    for (i = 0; i < n; i++ )
    scanf("%f", &num[i]);
    
    // Calculating Mean
    for (i = 0; i < n; i++)
    sum += num[i];
    mean = sum / n;
    
    // Calculating Standard Deviation
    sum = 0.0;
    for (i = 0; i < n; i++)
    sum += (num[i] - mean) * (num[i] - mean);
    SD = sqrt(sum / n);
    printf("Mean: %6.3f \n", mean);
    printf("Standard Deviation: %.6f", SD);
    return 0;
}

Output

Enter total number of elements: 10
Enter the value of elements: 
1
2
3
4
5
6
7
8
9
10
Mean:  5.500 
Standard Deviation: 2.872281

How Does This Program Work ?

    int i, n;
    float num[25];
    float sum = 0.0, mean, SD;

In this program, we have declared two int data type variables named i and n. We have also declared four float data type variables named as sum, mean, num and SD.

    // Asking for input
    printf("Enter total number of elements: ");
    scanf("%d", &n);
    printf("Enter the value of elements: \n");
    for (i = 0; i < n; i++ )
    scanf("%f", &num[i]);

Then, the user is asked to enter the total number of elements and value of each of the elements.

    // Calculating Mean
    for (i = 0; i < n; i++)
    sum += num[i];
    mean = sum / n;

We calculate the Mean by adding all the elements and dividing that number by the total number of elements.

    // Calculating Standard Deviation
    sum = 0.0;
    for (i = 0; i < n; i++)
    sum += (num[i] - mean) * (num[i] - mean);
    SD = sqrt(sum / n);

Now, we subtract each element by the mean and square the result. Then, we take out the square root of that number(Mean of Squared Differences). Hence, we obtain the standard Deviation. 

    printf("Mean: %6.3f \n", mean);
    printf("Standard Deviation: %.6f", SD);

Finally, the value of Mean and Standard Deviation is printed on the screen using printf() function.

Conclusion

I hope after going through this post, you understand how to calculate standard deviation using C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

1 thought on “C Program To Calculate Standard Deviation”

  1. In calculation of standard deviation in line fourth SD = sqrt(sum / n); is wrong instead it should be SD = sqrt(sum / n-1); for correct calculation of SD. THANKS IT HELP ME TO FIGURE ME OUT MY ERROR.

Leave a Comment

Your email address will not be published. Required fields are marked *