Q.Write an algorithm that performs the following:
Ask a user to enter a number. If the number is between 5 and 15, write the word GREEN. If the number is between 15 and 25, write the word BLUE. if the number is between 25 and 35, write the word ORANGE. If it is any other number, write that ALL COLOURS ARE BEAUTIFUL.
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 →A multi-way if / else-if ladder tests the number against each range in turn — GREEN for 5–15, BLUE for 15–25, ORANGE for 25–35, otherwise "ALL COLOURS ARE BEAUTIFUL".
The idea (multi-way selection). When one input must be classified into several mutually exclusive outcomes, a chain of ELSE IF branches is the right tool: the conditions are tested top-down and exactly one branch executes. Note the question's wording ("between 5 and 15", "between 15 and 25") overlaps at 15 and 25; an else-if ladder resolves this cleanly because the first matching branch wins — 15 falls to GREEN, 25 to BLUE. This is a reasonable and stated design choice, not an accident.
Algorithm
COLOUR_ALGO
Step 1: INPUT num
Step 2: IF num >= 5 AND num <= 15 THEN
PRINT "GREEN"
ELSE IF num > 15 AND num <= 25 THEN
PRINT "BLUE"
ELSE IF num > 25 AND num <= 35 THEN
PRINT "ORANGE"
ELSE
PRINT "ALL COLOURS ARE BEAUTIFUL"
END IF
Step 3: END
Python verification
num = float(input("Enter a number: "))
if 5 <= num <= 15:
print("GREEN")
elif 15 < num <= 25:
print("BLUE")
elif 25 < num <= 35:
print("ORANGE")
else:
print("ALL COLOURS ARE BEAUTIFUL")
Test table
| Input | Branch taken | Output |
|---|---|---|
| 10 | 5 ≤ 10 ≤ 15 | GREEN |
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.