Algorithms & Flowcharts — Worked Examples
A hands-on workshop of fully worked algorithm examples. For every problem you get a flowchart, pseudocode, a step-by-step trace table, and the Python code — so you can see exactly how an idea becomes a working program. This is the heart of building a strong programming foundation.
5. Making Decisions II
Making Decisions II
Now we combine several decisions to solve richer problems. The same rule still applies: every diamond asks one yes/no question and has exactly two labelled exits. Here are three worked examples.
Example (a): Largest of three numbers
Problem
Read three numbers and display the largest. We keep a variable largest, start it at the first number, then compare it with the other two, updating it whenever we find something bigger.
Pseudocode
START
INPUT a, b, c
SET largest = a
IF b > largest THEN
SET largest = b
END IF
IF c > largest THEN
SET largest = c
END IF
OUTPUT largest
END
Trace table
Sample input: a = 3, b = 9, c = 5.
| Step | a | b | c | largest | Decision |
|---|---|---|---|---|---|
| INPUT a, b, c | 3 | 9 | 5 | — | — |
| largest = a | 3 | 9 | 5 | 3 | — |
| IF b > largest | 3 | 9 | 5 | 3 | 9 > 3 → Yes |
| largest = b | 3 | 9 | 5 | 9 | — |
| IF c > largest | 3 | 9 | 5 | 9 | 5 > 9 → No |
| OUTPUT largest | 3 | 9 | 5 | 9 | — |
In Python
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
largest = a
if b > largest:
largest = b
if c > largest:
largest = c
print("The largest is", largest)
Output: (with a 3, b 9, c 5)
The largest is 9
Example (b): Student grade calculator
Problem
Read a test score from 0 to 100 and print a letter grade: A for 90+, B for 80–89, C for 70–79, D for 60–69, otherwise F. This is a ladder of decisions checked from the top down.
Pseudocode
START
INPUT score
IF score >= 90 THEN
SET grade = "A"
ELSE IF score >= 80 THEN
SET grade = "B"
ELSE IF score >= 70 THEN
SET grade = "C"
ELSE IF score >= 60 THEN
SET grade = "D"
ELSE
SET grade = "F"
END IF
OUTPUT grade
END
Trace table
Sample input: score = 85.
| Step | score | Test | Result | grade |
|---|---|---|---|---|
| INPUT score | 85 | — | — | — |
| IF score ≥ 90 | 85 | 85 ≥ 90 | No | — |
| ELSE IF score ≥ 80 | 85 | 85 ≥ 80 | Yes | — |
| grade = "B" | 85 | — | — | B |
| OUTPUT grade | 85 | — | — | B |
In Python
score = int(input("Enter the score (0-100): "))
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print("Grade:", grade)
Output: (with score 85)
Grade: B
Example (c): Leap-year check
Problem
Read a year and decide whether it is a leap year. The rule: a year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years — unless they are also divisible by 400 (then they are).
Pseudocode
START
INPUT year
IF year mod 4 = 0 THEN
IF year mod 100 = 0 THEN
IF year mod 400 = 0 THEN
OUTPUT "Leap year"
ELSE
OUTPUT "Not a leap year"
END IF
ELSE
OUTPUT "Leap year"
END IF
ELSE
OUTPUT "Not a leap year"
END IF
END
Trace table
Sample input: year = 2000.
| Step | year | Test | Result | Output |
|---|---|---|---|---|
| INPUT year | 2000 | — | — | — |
| IF year mod 4 = 0 | 2000 | 2000 mod 4 = 0 | Yes | — |
| IF year mod 100 = 0 | 2000 | 2000 mod 100 = 0 | Yes | — |
| IF year mod 400 = 0 | 2000 | 2000 mod 400 = 0 | Yes | — |
| OUTPUT | 2000 | — | — | Leap year |
In Python
year = int(input("Enter a year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Output: (with year 2000)
2000 is a leap year
Experienced programmers often fold this into one line — if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0): — but the nested version matches the flowchart step by step, which makes the rule easier to learn.