C Program to Find Mean, Variance and Standard Deviation of Array

Write C Program to Find Mean, Variance and Standard Deviation of Array

// C Program to Find Mean, Variance and Standard Deviation of Array

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

int main()
{
	int A[] = {21, 23, 41, 62, 32};
	int n = sizeof(A)/sizeof(A[0]), i;

	float sum = 0, Var = 0, Mean, SD;

	printf("Array A :--> ");
	for(i=0; i<n; i++)
        printf("%d  ", A[i]);


	for(i = 0;i < n; i++)      //to find the mean
	{
		sum = A[i] + sum;
	}

	Mean = sum/n;
	printf("\n\nSum :--> %0.2f", sum);
	printf("\nMean :--> %0.2f", Mean);


	for(i=0;i<n;i++)	//to find the variance
	{
		Var = Var + ((A[i] - Mean) * (A[i]- Mean)) / (n-1);
	}
	printf("\nVariance :--> %0.2f", Var);


	SD = sqrt(Var);
	printf("\nStandard Deviation :--> %0.2f", SD);
	return 0;
}

Output:

Array A :--> 21  23  41  62  32

Sum :--> 179.00
Mean :--> 35.80
Variance :--> 277.70
Standard Deviation :--> 16.66

≪ Previous | Next ≫