C Program to Find Mean, Variance and Standard Deviation

«
»

This is a C Program to calculate the mean, variance & standard deviation.

Problem Description

This C Program calculates the mean, variance & standard deviation.

Problem Solution

The formula which is used in this program is mean = average of the numbers. variance = (summation( ( Xi – average of numbers) * ( Xi – average of numbers)) ) / Total no of elements. where i = 1 to N here N is the total no of elements. Standard deviation = Squareroot of the variance.

Program/Source Code

Here is source code of the C program to calculate the mean, variance & standard deviation. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to input real numbers and find the mean, variance
 * and standard deviation
 */
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
 
void main()
{
    float x[MAXSIZE];
    int  i, n;
    float average, variance, std_deviation, sum = 0, sum1 = 0;
 
    printf("Enter the value of N \n");
    scanf("%d", &n);
    printf("Enter %d real numbers \n", n);
    for (i = 0; i < n; i++)
    {
        scanf("%f", &x[i]);
    }
    /*  Compute the sum of all elements */
    for (i = 0; i < n; i++)
    {
        sum = sum + x[i];
    }
    average = sum / (float)n;
    /*  Compute  variance  and standard deviation  */
    for (i = 0; i < n; i++)
    {
        sum1 = sum1 + pow((x[i] - average), 2);
    }
    variance = sum1 / (float)n;
    std_deviation = sqrt(variance);
    printf("Average of all elements = %.2f\n", average);
    printf("variance of all elements = %.2f\n", variance);
    printf("Standard deviation = %.2f\n", std_deviation);
}
Program Explanation

In this C Program, we are reading the number of values using ‘n’ variable. Using for loop we are entering the real numbers to compute the mean, variance and standard deviation of the number.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

For loop is used to calculate the sum of all elements. Compute the average of the value of ‘sum’ variable by the number of elements present in the ‘n’ variable.

Find the variance and standard deviation of the elements. The following formula is used
Variance = (summation ((X[i] – average of numbers) * (X[i] – average of numbers))) / Total number of elements,

Where i = 1 to N here N is the total number of elements

Standard deviation = Squareroot of the variance value.

Runtime Test Cases
 
$ cc pgm23.c -lm
$ a.out
Enter the value of N
5
Enter 5 real numbers
34
88
32
12
10
Average of all elements = 35.20
variance of all elements = 794.56
Standard deviation = 28.19

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.