Header Ads

06 - WAP to Print the Value of a Median in the given Number in C

Program to Print the Value of a Median in a given set of Number's in an Array.

//to check the median of a given Numbers in C
#include 
int main()
{
       int x, idx ;
       float avg;
       //Enter the total number of elements
       printf("Enter Total Number of Elements, to be checked : ");
       scanf("%d",&x);
       int array [x];
       printf("Enter the '%d' Elements (with space): ", x);
       //loop to store the values in the loop
       for (idx = 0; idx < x; idx++)
       {
            scanf("%d",&array[idx]);
       }

 //this is to check the no of elements is even or odd
 //for even noof element first one will run for odd else will execute
      if ( x % 2 == 0 )
      {
           avg = (array[ x / 2 ] + (array[ x / 2 ] - 1))/2.0;
           printf("Median is : %f\n",avg); 
      }
      else
      {
      printf ("Median is : %d\n", array[ x / 2 ]);
      }
   return 0;
}

Program Output:

No comments