Thursday, 15 August 2019

C programming for mean, median, mode


/*To find the mean, median, mode*/


#include<stdio.h>
void
main
()
{
            int i,j,n;
            float a[100],t,sum=0.0,mean,median,mode;
            printf("Enter the total no. of data :");
            scanf("%d",&n);
            printf("Type the input data:\n");
            for(i=0;i<n;i++)
            {
                        scanf("%f",&a[i]);
                        sum=sum+a[i];
            }
            mean=sum/n;
            printf("mean=%f\n",mean);
            /*Sorting array*/
            for(i=0;i<n-1;i++)
            {for(j=i+1;j<n;j++)
            {if (a[i]>a[j])
            {t=a[i];
            a[i]=a[j];
            a[j]=t;
            }
            }
            }
            for(i=0;i<n;i++)
            {
                        printf ("%f\n",a[i]);
            }
            if (n%2==0)
                        median=(a[n/2]+a[(n-2)/2])/2;
            else
                        median=a[(n-1)/2];
            mode=3*median-2*mean;
                        printf("median=%f\n",median);
            printf("mode=%f\n",mode);
}

No comments:

Post a Comment

Thanks for comment.