Wednesday, 11 January 2012

Calculate Mean, Variance, Standard Deviation of some Numbers

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

void main()
{
    float n,i,mean,var,sd,a[100],temp=0,sum=0;
    clrscr();
    printf("Enter the number of terms::\n");
    scanf("%f",&n);
    printf("Enter the numbers::\n");
    for(i=0;i<n;i++)
    {
        scanf("%f",&a[i]);
    }

    /*..............Mean.............*/
    for(i=0;i<n;i++)
    {
        sum=sum+a[i];
    }
    mean=sum/n;
    printf("Mean=%f\n",mean);

    /*.............Variance..........*/
    for(i=0;i<n;i++)
    {
        temp=temp+(a[i]-mean)*(a[i]-mean);
    }
    var=temp/n;
    printf("Variance=%f\n",var);

    /*............Standard Deviation........*/
    sd=sqrt(var);
    printf("Standard Deviation=%f\n",sd);
    getch();
}

No comments:

Post a Comment