using System; public class Test { public static decimal GetMedian(int[] array) { int[] tempArray = array; int count = tempArray.Length; Array.Sort(tempArray); decimal medianValue = 0; if (count % 2 == 0) { // count is even, need to get the middle two elements, add them together, then divide by 2 int middleElement1 = tempArray[(count / 2) - 1]; int middleElement2 = tempArray[(count / 2)]; medianValue = (middleElement1 + middleElement2) / 2; } else { // count is odd, simply get the middle element. medianValue = tempArray[(count / 2)]; } return medianValue; } public static void Main() { Console.Write("Median Value: "); int[] items = new int[] {12, 5, 2, 16}; var median = GetMedian(items); Console.WriteLine(median); } }