NCERT Computer Science Class #11 – Chapter #6 Solutions – Q #8

Chapter #6 – Flow of Control

NCERT Computer Science Class #11 – Chapter #6 Solutions – Q #8

Class 11 – Chapter 6 Solutions – PROGRAMMING EXERCISES #3

Write a program that prints minimum and maximum of five numbers entered by the user.



Python Program

def find_min_max(numbers):
    """Finds and returns the minimum and maximum values from a list of numbers."""
    min_value = min(numbers)
    max_value = max(numbers)
    return min_value, max_value

# Get user input
numbers = []

print("Enter five numbers:")
for i in range(5):
    while True:
        try:
            num = float(input(f"Number {i + 1}: "))  # Accepting float for more flexibility
            numbers.append(num)
            break  # Exit loop if input is valid
        except ValueError:
            print("Please enter a valid number.")

# Find minimum and maximum
min_value, max_value = find_min_max(numbers)

# Print results
print(f"The minimum value is: {min_value}")
print(f"The maximum value is: {max_value}")

You can checout this code from Github here – Programming Exercise – 3

Explanation of the Code:

1.    Function Definition:
      •	find_min_max(numbers): This function takes a list of numbers as input and 
        uses the built-in min() and max() functions to find the minimum and maximum 
        values, respectively.
2.    User Input:
      •	The program initializes an empty list called numbers.
      •	It prompts the user to enter five numbers in a loop.
      •	A nested while loop ensures that the program continues to prompt for input 
        until a valid number is entered. It uses try and except to handle invalid 
        inputs gracefully.
3.    Finding Min and Max:
      •	After collecting all five numbers, the program calls the find_min_max() 
        function to get the minimum and maximum values.
4.    Output:
      •	Finally, it prints out the minimum and maximum values.

Example Output:

Enter five numbers:
Number 1: 1
Number 2: 2
Number 3: 3
Number 4: 4
Number 5: 5
The minimum value is: 1.0
The maximum value is: 5.0

NCERT Computer Science Class 11 Chapter 6 Solutions Q 8

Posts created 29

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top