Step 01 Beginner Problems — Fundamentals of Programming
Flowchart Problem-Solving
Knowing flowchart symbols is useless without a method for applying them. This lesson works through the classic beginner problems — reversing a number, GCD, counting digits — the flowchart way: identify input and output, find the repeating step, choose the decision that stops it, then trace with real numbers. These three problems share one skeleton, and recognising skeletons is the whole game in DSA.
The four questions that build any flowchart
Ask, in order: (1) What comes in, and what must go out? (2) If I did this by hand, what step would I repeat? That repetition is your loop body. (3) How do I know when to stop repeating? That is your decision diamond. (4) What values must I remember between repetitions? Those are your variables, and they must be initialised before the loop.
Almost every beginner flowchart bug is one of these four questions left unanswered — most often (4), the missing initialisation.
Worked problem 1: reverse a number
Input: 1234. Output: 4321. By hand you peel digits off the right (4, 3, 2, 1) and build the answer by shifting left and appending: 0 → 4 → 43 → 432 → 4321. The repeated step is 'take last digit (n mod 10), append it (rev = rev * 10 + digit), drop it (n = n div 10)'. Stop when n becomes 0. Variables: rev starts at 0, and a copy of the input is unneeded here because the original is not used afterwards.
The same skeleton with a different 'append' rule solves a family: count digits (increment a counter instead), sum of digits (add the digit instead), largest digit (keep a maximum instead). One loop, four problems — this is why the digit-extraction loop is worth mastering first.
Worked problem 2: GCD by Euclid's idea
GCD(48, 18): repeatedly replace the larger number by the remainder of the division — (48, 18) → (18, 12) → (12, 6) → (6, 0) — and the last non-zero value, 6, is the answer. In flowchart terms: a decision 'is b equal to 0?' guards a loop whose body is (a, b) = (b, a mod b).
Trace a second pair on paper before coding: GCD(17, 5) → (5, 2) → (2, 1) → (1, 0) → answer 1. If your trace and your code ever disagree, the trace is right and the code is wrong — that is the entire philosophy of the next lesson.
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
print(gcd(48, 18)) # 6
print(gcd(17, 5)) # 1From flowchart to code without tears
Translate mechanically: each rectangle becomes a statement, each diamond becomes an if or a while condition, each loop-back arrow becomes the loop's closing. Then trace the code with the same numbers you used on paper, writing variable values in a small table. If the table matches your hand trace, ship it.
Do not 'improve' the code while translating — optimise later, on a working solution. Most beginner disasters come from editing logic mid-translation and losing track of which version was correct.
Key points
- Build any flowchart by answering four questions: inputs/outputs, the repeated step, the stopping decision, the variables to remember.
- Digit problems share one skeleton: digit = n % 10, update an accumulator, n = n // 10, stop at 0.
- Changing only the accumulator turns that skeleton into count digits, sum of digits, reverse, or largest digit.
- Euclid's GCD loop: (a, b) = (b, a % b) until b is 0; trace it on paper before coding it.
- Translate flowcharts mechanically, then verify code against your paper trace — never optimise mid-translation.