Sunday 18 August 2019

C program for calculation of mean, median, mode


/*calculation of 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 number 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(j=0;j<n;j++)
                {
                                for(i=0;i<n;i++)
                                {
                                                if(a[i]>a[j])
                                                {t=a[i];
                                                a[i]=a[j];
                                                a[j]=t;
                                                }
                                }
                }
                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);
}
RESULT1:
enter the total number of data3
type the input data:
1
4
3
mean=2.666667
median=3.000000
mode=3.666667
Press any key to continue
RESULT2:
enter the total number of data 10
type the input data:
2
4
6
5
8
1
0
15
66
40
mean=14.700000
median=5.500000
mode=-12.900000
Press any key to continue
RESULT3:
enter the total number of data6
type the input data:
2
25
13
7
21
4
mean=12.000000
median=10.000000
mode=6.000000
Press any key to continue

No comments:

Post a Comment

Thanks for comment.