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.
13. From Flowchart to Code: The Bridge
The one skill that ties it all together
A flowchart shows what should happen; code tells the computer how. Once you see that every flowchart shape maps to a small, predictable piece of Python, translation stops being mysterious. This chapter makes that mapping explicit and then walks a single problem all the way across the bridge.
Symbol / construct → Python mapping
| Flowchart symbol | Pseudocode construct | Python equivalent |
|---|---|---|
| Oval (terminator) | START / END, RETURN | def name(...): … return |
| Rectangle (process) | assignment / calculation | a statement, e.g. total = total + i |
| Diamond (decision) | IF / ELSE IF / ELSE | if / elif / else: |
| Parallelogram (I/O) | INPUT / OUTPUT | input() / print() |
| Arrow looping back | WHILE / REPEAT / FOR | while condition: / for x in ...: |
| Connector into a box | sequence (next step) | the next line of code (indentation) |
Worked example: sum of even numbers from 1 to N
Problem
Read a number N, then add up every even number from 1 to N and print the total. For N = 6 the even numbers are 2, 4, 6, so the answer is 12.
Pseudocode
SUM_EVENS:
N = INPUT
total = 0
i = 1
WHILE i <= N:
IF i is even:
total = total + i
i = i + 1
OUTPUT total
Trace table
Tracing with N = 6:
| i | i <= 6? | i even? | total |
|---|---|---|---|
| 1 | Yes | No | 0 |
| 2 | Yes | Yes | 2 |
| 3 | Yes | No | 2 |
| 4 | Yes | Yes | 6 |
| 5 | Yes | No | 6 |
| 6 | Yes | Yes | 12 |
| 7 | No → stop | — | 12 |
In Python
Notice how each flowchart shape became one line, following the mapping table above:
def sum_evens(n):
total = 0 # process box
i = 1 # process box
while i <= n: # loop-back arrow
if i % 2 == 0: # decision diamond
total += i # process box
i += 1 # process box
return total # terminator
N = 6 # stands in for input()
print(sum_evens(N))
Output:
12
Practice challenges
Now try the whole bridge yourself. For each problem below, draw a flowchart, write pseudocode, build a trace table on a sample input, then code it in Python:
- Read a number and print whether it is odd or even.
- Read 5 numbers and print their average.
- Print the multiplication table (1 to 12) for a number the user chooses.
- Read a number N and print its factorial (N! = 1×2×…×N).
- Count how many vowels are in a word the user types.
- Decide whether a given year is a leap year.
- Read a list of marks and print how many are a pass (≥ 40) and how many fail.
- Check whether a short word is a palindrome (reads the same backwards).