wiz-icon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

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

Open in App
Solution

Program:
#Program to input five numbers and print largest and smallest numbers
smallest = 0
largest = 0
for a in range(0,5):
x = int(input("Enter the number: "))
if a == 0:
smallest = largest = x
if(x < smallest):
smallest = x
if(x > largest):
largest = x
print("The smallest number is",smallest)
print("The largest number is ",largest)

Explanation of Program:
The first ‘if’ loop will assign the first value entered by the user to the smallest and largest variable.

The subsequent if loop will check the next number entered and if it is smaller than the previous value, it will be assigned to ‘smallest’ variable, and if greater than the previous value it will be assigned to 'largest' variable.

After the end of the loop, the values of the ‘smallest’ and ‘largest’ variable are printed.

OUTPUT:
Enter the number: 10
Enter the number: 5
Enter the number: 22
Enter the number: 50
Enter the number: 7
The smallest number is 5
The largest number is 50


flag
Suggest Corrections
thumbs-up
24
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Graphical Representation of Data
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon