Question

Write a program to read a list of $n$ integers and find their median.

Note: The median value of a list of values is the middle one when they are arranged in order. If there are two middle values then take their average.

Hint: You can use an built-in function to sort the list

✨ Free AI Tools for You

Powered by Chatterbot AI

Answer

Here is a Python program that reads a list of ( n ) integers from the user and computes the median of the list:

def find_median(nums):
    # Sort the list
    nums.sort()
    
    # Find the length of the list
    n = len(nums)
    
    # If the number of elements is odd, return the middle element
    if n % 2 != 0:
        median = nums[n // 2]
    else:
        # If the number of elements is even, return the average of the two middle elements
        mid1 = nums[n // 2]
        mid2 = nums[(n // 2) - 1]
        median = (mid1 + mid2) / 2
    
    return median

# Read the list of numbers from the user
n = int(input("Enter the number of elements: "))
nums = []
for i in range(n):
    elem = int(input(f"Enter element {i+1}: "))
    nums.append(elem)

# Find and print the median
median = find_median(nums)
print(f"The median of the list is: {median}")

Explanation:

  1. The find_median function takes a list of integers as input and sorts it.

  2. The median is found by checking if the length of the list (n) is odd or even:

    • If odd, the middle element is returned.

    • If even, the average of the two middle elements is returned.

  3. The user is prompted to enter the number of elements and then the individual elements of the list.

  4. Finally, the program computes and prints the median of the list.


Was this helpful?

India's 1st AI Doubt Solver for CBSE, JEE, and NEET

Ask a Question for Free

and then it's just ₹212 a month

Learn how Chatterbot AI can help you succeed

Cover Image of the article Computer Science Class 11 CBSE - The Ultimate Guide with Notes, Solutions and AI

Computer Science Class 11 CBSE - The Ultimate Guide with Notes, Solutions and AI

This ultimate guide for CBSE Computer Science class 11 has detailed notes, NCERT solutions, cheat sheets, and our free AI-powered doubt-solving assistant, Chatterbot AI.

Cover Image of the article JEE Advanced 2024 Exam Date Announced: Complete Guide to Eligibility, Syllabus, and Preparation Tips

JEE Advanced 2024 Exam Date Announced: Complete Guide to Eligibility, Syllabus, and Preparation Tips

JEE Advanced 2024 on May 26! Get exam schedule, syllabus, prep tips & more in this guide. Ace India's top engineering test with topper strategies.

Cover Image of the article How to Crack NEET: The Ultimate Blueprint to Outsmart the Exam and Unlock Your Medical Dreams

How to Crack NEET: The Ultimate Blueprint to Outsmart the Exam and Unlock Your Medical Dreams

Ace NEET with expert strategies: Discover effective exam strategies, time management, core concepts mastery, problem-solving techniques, revision tips, and AI-assisted doubt clearing with Chatterbot AI.

Cover Image of the article How to Crack IIT: Smart Self-Study Strategies and AI Tools for Success

How to Crack IIT: Smart Self-Study Strategies and AI Tools for Success

Ace IIT JEE in 6 months without coaching. Discover expert self-study strategies for Physics, Chemistry, and Math. Master time management, mock tests, and leverage AI tools like Chatterbot AI for personalized doubt-solving.