Question 8 of 13intermediate🔧 ApplyLong Answer4 marks

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

Correct Answer

Program to find minimum and maximum of five numbers

Initialize counter and take first number as input

count = 1 num = int(input("Enter number 1: ")) minimum = num maximum = num

Loop to input remaining four numbers and compare

while count < 5: num = int(input("Enter number " + str(count + 1) + ": ")) if num < minimum: minimum = num if num > maximum: maximum = num count += 1

Display the results

print("Minimum number is:", minimum) print("Maximum number is:", maximum)

Exercise: PROGRAMMING EXERCISES | Q: 3
For More Understanding

Explanation

This program uses a while loop to accept five numbers from the user. The first number initializes both minimum and maximum variables. As each subsequent number is entered, it is compared with the current minimum and maximum values. If the new number is smaller than minimum, the minimum variable is updated. Similarly, if the new number is larger than maximum, the maximum variable is updated. After all five numbers are processed, the program displays the minimum and maximum values. This approach efficiently tracks both extremes in a single pass through the data.

Solution Steps

  1. Step 1: Initialize counter to 1 and take first number input to set initial minimum and maximum values.

  2. Step 2: Use a while loop to iterate for the remaining four numbers (while count < 5).

  3. Step 3: Inside the loop, compare each new number with minimum and maximum, updating them if necessary.

  4. Step 4: Increment the counter after each iteration using count += 1.

  5. Step 5: After the loop ends, print the minimum and maximum values using print statements.