This is a C Program to calculate the mean, variance & standard deviation.
This C Program calculates the mean, variance & standard deviation.
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.
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); }
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.
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.
$ 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.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Check C Books
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Apply for C Internship
- Check Computer Science Books