C Program to Find Mean, Variance and Standard Deviation

This C program calculates the mean, variance, and standard deviation of a set of numbers. These statistical measures are commonly used in data analysis and statistics to describe the central tendency, spread, and dispersion of a dataset.

Problem statement

Write a C program that takes a set of numbers as input and calculates the mean, variance, and standard deviation of the given set.

C Program to Find Mean, Variance and Standard Deviation

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

int main() {
    int n, i;
    float sum = 0.0, mean, variance = 0.0, stdDeviation;

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

    float numbers[n];

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

    mean = sum / n;

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

    variance /= n;
    stdDeviation = sqrt(variance);

    printf("Mean: %.2f\n", mean);
    printf("Variance: %.2f\n", variance);
    printf("Standard Deviation: %.2f\n", stdDeviation);

    return 0;
}

How It Works

  1. The user is prompted to enter the number of elements, in this case, 5.
  2. The program then asks the user to input each element of the dataset.
  3. The sum of all the elements is calculated to find the mean.
  4. The variance is calculated by subtracting the mean from each element, squaring the result, and summing up the squares.
  5. The variance is divided by the number of elements to get the average variance.
  6. The standard deviation is calculated as the square root of the variance.
  7. The program finally displays the calculated mean, variance, and standard deviation.

Input\output

C Program to Find Mean, Variance and Standard Deviation

Share:

Leave A Reply

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

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...