Exercise · Q10
Q.Write an algorithm that accepts four numbers as input and find the largest and smallest of them.
Bihar BsebTextbookSubjective· 3mImportance★★★★★est
78% · 28/36 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Assume the first number is both the largest and the smallest, then let each remaining number challenge those titles — six comparisons find both answers.
The idea (running maximum/minimum). The standard technique for extremes is: seed the candidate with the first value, then compare every other value against it and replace when beaten. Doing this simultaneously for large and small classifies all four inputs in one pass.
Algorithm
LARGEST_SMALLEST_OF_FOUR
Step 1: INPUT a, b, c, d
Step 2: SET large = a
Step 3: SET small = a
Step 4: IF b > large THEN large = b
Step 5: IF c > large THEN large = c
Step 6: IF d > large THEN large = d
Step 7: IF b < small THEN small = b
Step 8: IF c < small THEN small = c
Step 9: IF d < small THEN small = d
Step 10: PRINT "Largest =", large
Step 11: PRINT "Smallest =", small
END
Python verification
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
d = float(input("Enter fourth number: "))
large = small = a
if b > large: large = b
if c > large: large = c
if d > large: large = d
if b < small: small = b
if c < small: small = c
if d < small: small = d
print("Largest =", large)
print("Smallest =", small)
Dry run for inputs 12, 5, 29, 17:
| Step | Comparison | large | small | …
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.