Tuesday , 19 December 2017
Home » Tutorials » C programming » C program to find Mean, Median and mode with algorithm

C program to find Mean, Median and mode with algorithm

Well before starting, let’s look at the basic defention and how we find mean, median and mode. Both these are 3 variables to find the average value or an approximate value of a list, but yet they have different values as they are based on 3 different concepts

Concepts of mean, median and mode

What is mean ? 

Mean is same as average. The mean is found by adding up all of the given data and dividing by the number of elements.

Example:  Mean of 1,2,3,4,5 is
(1+2+3+4+5 )/5 = 15/3 = 3

What is Median ?

The median is the middle number in an ordered list (ascending or descending).  First you arrange the numbers in orders in ascending order, then you find the middle number and save it as median

Example:
1 2 3 4 5

Median is 3

 

What is Mode ?

Mode is the element which happens most number of time in the list. If no element happens more than once, all elements are considered as mode.

Algorithm 

  1. Start
  2. Declare an array a[20], sum =0,i=0,j=0,z=0
  3. Read number of terms n
  4. Repeat step 5 & 6 while (i<n)
  5. sum=sum+a[i]
  6. next i
  7. mean=sum/n
  8. if (n%2=0)
    median=(a[n/2] +a[n/2-1])/2
    else
    median=a[n/2]
  9. print mean and median
  10. repeat step 11 &  12 when i=0 to n, j=0 to i
  11. if a[i]=a[j], then b[i]++
  12. if b[i]>z, then z=b[i]
  13. For i=0 to n if b[i]=z, print b[i]
  14. end

Program

 #include <stdio.h>
main()
{ int i,j,x,k=0,n,a[20],z=0,b[20];
float sum=0, t,mean,medn,mod;
printf(“\n Enter the no. of elements (Max 20)\n”);
scanf(“%d”,&n);
printf(“Enter the elements\n”);
for(i=0;i
{scan (“%d”,&a[i]);}
for(i=0;i
{sum=sum+a[i];}
printf(“Sum: %f”,sum);
mean=sum/n;
}
for(i=0;i
{ for (j=i+1;j
  { if (a[i]>a[j])
      { t=a[i];a[i]=a[j];a[j]=t; }
 }
}
if (n%2==0)
 medn=(a[n/2]+a[(n/2)-1])/2;
else
medn=a[n/2];
for(i=0;i
{ for ( j=0; j<i; j++ )
  { if (a[i]==a[j])
      b[i]++;
  }
}
for(i=0;i<n;i++)
{ if (b[i]>z)
   z=b[i];
}
printf(“Mean :%f \n Median : %f \n Mean : “, mean, medn);
for I i=0; i<n; i++)
 { if (b[i]==z)
    printf(“%d\t”, a[i]);
}
}

 

About Rajeel

I'm Rajeel, a teen blogger from India who loves computers, sports, internet and all other kind of tech stuffs. This blog thing is one of my favourite hobby and the one that eats up much of my time. But Really, I'm loving it :) Find me in G+

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Scroll To Top