This post helps calculate the standard deviation using the C programming language.

C Program to Calculate Standard Deviation

#include<stdio.h>
#include<math.h>
float CSD(float data[])
{
    float sum=0.0, mean, SD=0.0;
    int i;
    for(i=0;i<5;i++)
    {
        sum+=data[i];
        mean=sum/5;
    }
    for(i=0;i<5;i++)
    SD+=pow(data[i]-mean,2);
    return sqrt(SD/5);
}
int main()
{
    int i;
    float data[5];
    printf("Enter 5 elements: \n");
    for(i=0;i<5;i++)
    scanf("%f",&data[i]);
    printf("Standard Deviation=%.3f", CSD(data));
    return 0;
}

Output

Explanation

In this C program, we deal with standard deviation, where it is a measure of how to spread out numbers are. Its symbol is sigma. The formula is the square root of the Variance.

We first start with the function where the calculations are done. The function is named as CSD (Calculate Standard Deviation), the data types used are float and integer. Here the calculation is executed using variables summean, and SD (Standard Deviation).

float CSD(float data[])
{
    float sum=0.0, mean, SD=0.0;
    int i;
    for(i=0;i<5;i++)
    {
        sum+=data[i];
        mean=sum/5;
    }
    for(i=0;i<5;i++)
    SD+=pow(data[i]-mean,2);
    return sqrt(SD/5);
}

Now we pass on to the main function where the user is allowed to assign values for the variables and data. The For loop checks the given condition to proceed further for the desired output. Here we have stated “%.3f” which indicates the decimal place after three points.

int main()
{
    int i;
    float data[5];
    printf("Enter 5 elements: \n");
    for(i=0;i<5;i++)
    scanf("%f",&data[i]);
    printf("Standard Deviation=%.3f", CSD(data));
    return 0;
}

Now we have received the coveted output.

Leave a Reply

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

Liked this content?. Please spread the word :)

  • RSS
  • Twitter
    Visit Us
    Follow Me