You are on page 1of 5

Here I have written this code and I have attached screenshot of this code output.

After text and screen shot I have also attached a link to check this code easily.
MEAN, MEDIAN, MODE in One C program
#include<stdio.h>
int main()
{
int i,n;
float array[100],sum=0.0;
printf("Number of elements of your array: ");
scanf("%d",&n);
while (n>100 || n<1)
{
printf("error! the range is 1 to 100. \n");
printf("enter the Number again: ");
scanf("%d",&n);
}
for(i = 0; i < n; i++)
{
printf("%d Enter Number: ",i+1);
scanf("%f",&array[i]);
sum = sum+array[i];
}
for(int i = 0; i < n; i++)
{
for(int j = 1; j < n; j++)
{
float temp;
if(array[j]<array[j-1])
{
temp=array[j-1];
array[j-1]=array[j];
array[j]=temp;
}
}
}
double mean = sum*1.0/n, median;
int mid = n/2;
if(n%2==0)
{
median = array[mid-1]+array[mid];
median = median / 2.0;
}
else median = array[mid];
float mode = array[0];
int cnt = 1, cnt1 = 1;
for(i = 0; i < n-1; i++)
{
if(array[i]==array[i+1])
{
cnt1=cnt1+1;
}
else
{
if(cnt<cnt1)
{
cnt = cnt1;
cnt1 = 1;
mode = array[i];
}
else cnt1 = 1;
}
}
if(cnt<cnt1)
{
cnt = cnt1;
mode = array[i];
}
if(cnt==1)
printf("Mean: %.2lf\nMedian: %.2lf\nMode: There is no mode\n",mean,median);
else
printf("Mean: %.2lf\nMedian: %.2lf\nMode: %.2lf\n",mean,median,mode);
return 0;
}
Fig no: 01 (test random number)

Fig no: 02(after run full code)


Fig no: 03 (this array is given)

To check this code please open this link: https://onlinegdb.com/7tmBX4w3V

THE END

You might also like