C program to calculate median of an array

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

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

Recommended Posts: