c
  1. c-program-to-calculate-standard-deviation

Program to Calculate Standard Deviation - (C Programs)

Example

#include <stdio.h>
#include <math.h>

int main() {
    int n, i;
    float data[100], mean, variance = 0, std_dev;

    printf("Enter the number of data items: ");
    scanf("%d", &n);

    printf("Enter data:\n");
    for(i=0; i<n; i++) {
        scanf("%f", &data[i]);
        mean += data[i];
    }

    mean /= n;

    for(i=0; i<n; i++) {
        variance += pow(data[i] - mean, 2);
    }
    variance /= n;

    std_dev = sqrt(variance);

    printf("Standard Deviation = %.2f", std_dev);
    return 0;
}

Output

Enter the number of data items: 5
Enter data:
1
2
3
4
5
Standard Deviation = 1.58

Explanation

The program calculates the standard deviation of a set of data. The formula to calculate the standard deviation is:

std_dev = sqrt(variance)

The variance is calculated using the following formula:

variance = sum((data[i] - mean)^2) / n

Where data[i] is the ith data item, mean is the mean of the data set, and n is the number of data items.

Use

The program can be used to calculate the standard deviation of a set of data. Standard deviation is a measure of the amount of variation or dispersion of a set of data values. It is widely used in statistical analysis to determine the reliability of a sample mean and to compare two data sets.

Summary

The program calculates the standard deviation of a set of data using the variance formula and the square root function. The resulting standard deviation can be used to determine the amount of variation in the data set. This program is useful for analyzing data sets and making statistical comparisons.

Published on: