Skip to content
Programming Exercises · Q3

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

Uttar Pradesh UpmspTextbookSubjective· 3mImportance★★★★★est
82% · 32/39 Questions
✓ Free question

Read the five numbers one by one and keep two "records" — smallest-so-far and largest-so-far — updating them on each input; print both after the loop.

The idea. We do not need to store all five numbers: to know the minimum and maximum it is enough to carry two running values and let every new input challenge them. This running-record pattern is the standard way to aggregate a stream of inputs with a loop.

Algorithm

  1. Start with smallest = None, largest = None (no record yet).
  2. Repeat 5 times: input a number; if it beats the current smallest (or there is no record yet) it becomes smallest; likewise for largest.
  3. After the loop, print both records.

Program

smallest = None
largest = None

for i in range(1, 6):
    num = int(input("Enter number " + str(i) + ": "))
    if smallest is None or num < smallest:
        smallest = num
    if largest is None or num > largest:
        largest = num

print("Minimum:", smallest)
print("Maximum:", largest)

Sample run with a step-by-step trace

Enter number 1: 34
Enter number 2: 12
Enter number 3: 78
Enter number 4: 5
Enter number 5: 56
Minimum: 5
Maximum: 78
Inputsmallest afterlargest after
343434
121234
781278
5578
56578

Key lines. The None sentinel means "no record yet", so the first number initialises both trackers without special-casing it. Both ifs are independent (not if–elif) because one input can break both records at once — as 34 does on the first pass.

Tip

If your class has covered lists, the same task is a two-liner: read the five numbers into a list nums and print min(nums), max(nums). The loop version shows the logic those built-ins hide.

✓Final answer

The program keeps running minimum and maximum while reading five numbers; for the sample inputs 34, 12, 78, 5, 56 it prints Minimum: 5 and Maximum: 78.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.