Java Program to Find the Median of an Array

Introduction

The median of an array is the middle element when the elements are sorted in ascending or descending order. If the array has an odd number of elements, the median is the middle element. If the array has an even number of elements, the median is the average of the two middle elements. This guide will walk you through writing a Java program that calculates the median of a given array.

Problem Statement

Create a Java program that:

  • Prompts the user to enter the size of an array and its elements.
  • Sorts the array.
  • Calculates and displays the median of the array.

Example:

  • Input: [1, 3, 4, 2, 7]

  • Output: "Median: 3"

  • Input: [1, 2, 3, 4, 5, 6]

  • Output: "Median: 3.5"

Solution Steps

  1. Read the Array Size and Elements: Use the Scanner class to take the size and elements of the array as input from the user.
  2. Sort the Array: Use the Arrays.sort() method to sort the array.
  3. Calculate the Median:
  • If the array size is odd, the median is the middle element.
  • If the array size is even, the median is the average of the two middle elements.
  1. Display the Result: Print the median value.

Java Program

// Java Program to Find the Median of an Array // Author: https://www.rameshfadatare.com/ import java.util.Arrays; import java.util.Scanner; public class FindMedian { public static void main(String[] args) { // Step 1: Read the size and elements of the array from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int size = scanner.nextInt(); double[] array = new double[size]; System.out.println("Enter the elements of the array:"); for (int i = 0; i < size; i++) { array[i] = scanner.nextDouble(); } // Step 2: Sort the array Arrays.sort(array); // Step 3: Calculate the median double median; if (size % 2 == 0) { // If the size is even, average the two middle elements median = (array[size / 2 - 1] + array[size / 2]) / 2; } else { // If the size is odd, take the middle element median = array[size / 2]; } // Step 4: Display the median System.out.println("Median: " + median); } } 

Explanation

Step 1: Read the Array Size and Elements

  • The Scanner class is used to read the size of the array and its elements. The nextDouble() method captures the size and each element, allowing the array to store both integer and decimal numbers.

Step 2: Sort the Array

  • The Arrays.sort() method is used to sort the array in ascending order.

Step 3: Calculate the Median

  • The program checks whether the size of the array is odd or even:
    • Odd Size: If the array size is odd, the median is the middle element of the sorted array (array[size / 2]).
    • Even Size: If the array size is even, the median is the average of the two middle elements ((array[size / 2 - 1] + array[size / 2]) / 2).

Step 4: Display the Result

  • The program prints the calculated median using System.out.println().

Output Example

Example 1:

Enter the size of the array: 5 Enter the elements of the array: 1 3 4 2 7 Median: 3.0 

Example 2:

Enter the size of the array: 6 Enter the elements of the array: 1 2 3 4 5 6 Median: 3.5 

Example 3:

Enter the size of the array: 4 Enter the elements of the array: 10 20 30 40 Median: 25.0 

Conclusion

This Java program demonstrates how to calculate the median of an array. The program covers essential concepts such as array manipulation, sorting, and basic arithmetic operations. This exercise is valuable for understanding how to handle statistical operations on arrays in Java programming.

Leave a Comment

Scroll to Top