Write a program that prints minimum and maximum of five numbers entered by the user.
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)
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
Step 1: Initialize counter to 1 and take first number input to set initial minimum and maximum values.
Step 2: Use a while loop to iterate for the remaining four numbers (while count < 5).
Step 3: Inside the loop, compare each new number with minimum and maximum, updating them if necessary.
Step 4: Increment the counter after each iteration using count += 1.
Step 5: After the loop ends, print the minimum and maximum values using print statements.