C Tutorial

#include<math.h> #include<stdio.h> #define MAX 10 void main() { int i, n; float num[MAX], deviation, sum, sumsqr, mean, variance, stddev; sum = 0; sumsqr =0; n = 0; printf("Enter number of elements:\n"); scanf("%d", &n); /* Reading array elements */ printf("Input %d values \n", n); for(i=0; i<n; i++) { scanf("%f", &num[i]); sum += num[i]; } mean = sum/(float)n; printf("Mean is %f\n", mean); for(i=0;i<n;i++) { deviation = num[i] - mean; sumsqr += deviation * deviation; } /* variance */ variance = sumsqr/(float)n; stddev = sqrt(variance); printf("Standard Deviation is %f\n", stddev); }Output:

$ cc standard-deviation.c -lm $ ./a.out Enter number of elements: 5 Input 5 values 32 35 31 33 34 Mean is 33.000000 Standard Deviation is 1.414214

C Program Examples

C Tutorial

Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page

^