C program to calculate median of an array

Editorial Staff - - C Programming Tutorial

To calculate the median from given array, First, you have to sort the given array either in ascending OR descending order. If the number of elements is even number, then the average of two numbers in the middle will be the median.

If the number of elements is the odd number, then the middle element of the array after sorting will be considered as the median.

In the below example, we have added 5 elements of the array.

Copy below c program and execute it with c compiler to see the output of the program.

Program Output

Enter the number of elements for the array : 5
Enter the elements for array_1..
array_1[0] : 35
array_1[1] : 54
array_1[2] : 31
array_1[3] : 22
array_1[4] : 85
The array after sorting is..
array_1[0] : 22
array_1[1] : 31
array_1[2] : 35
array_1[3] : 54
array_1[4] : 85
The median is : 35.000000

Editorial Staff

Editorial Staff at FreeWebMentor is a team of professional developers leads by Prem Tiwari

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.