Problem Solving
Algorithms, Flowcharts & Pseudocode
Think before you code: computational thinking, writing step-by-step algorithms, drawing flowcharts with the standard symbols, and writing clear pseudocode.
Most programming mistakes happen before any code is typed: the plan was wrong or vague. Professional developers plan solutions first. This lesson gives you three tools for planning.
Computational thinking
| Skill | Meaning | Example: "find the class topper" |
|---|---|---|
| Decomposition | break a big problem into smaller ones | read marks; compare; report |
| Pattern recognition | spot similarities with problems you have solved | "it's just finding a maximum" |
| Abstraction | ignore details that do not matter | names do not matter until the end |
| Algorithm design | write precise steps that always work | keep the best so far, update it |
Algorithms
An algorithm is a finite, ordered list of unambiguous steps that solves a problem. A recipe is an algorithm for cooking.
Algorithm: find the largest of three numbers
- Start.
- Read A, B, C.
- If A ≥ B and A ≥ C, then LARGEST = A.
- Otherwise, if B ≥ C, then LARGEST = B.
- Otherwise, LARGEST = C.
- Print LARGEST.
- Stop.
A good algorithm is correct for every input (what if two numbers are equal?), finite, unambiguous and efficient.
Flowcharts
A flowchart draws an algorithm as connected shapes:
| Symbol | Shape | Use |
|---|---|---|
| Terminal | rounded rectangle (oval) | Start / Stop |
| Input/Output | parallelogram | Read or print data |
| Process | rectangle | a calculation or assignment |
| Decision | diamond | a yes/no question, two exits |
| Flow line | arrow | the order of steps |
| Connector | small circle | joins parts of a long chart |
Loops appear as an arrow going back to an earlier step:
Pseudocode
Pseudocode writes an algorithm in structured, code-like English — precise like code, but free of any one language's syntax.
ALGORITHM SumOfDigits
INPUT n
sum ← 0
WHILE n > 0 DO
digit ← n MOD 10
sum ← sum + digit
n ← n DIV 10
END WHILE
OUTPUT sum
ENDCommon conventions: ← for assignment, IF … THEN … ELSE … END IF, FOR i ← 1 TO n DO … END FOR, WHILE … DO … END WHILE, INPUT and OUTPUT, and indentation to show blocks.
From plan to program
The pseudocode translates almost line by line into any language:
#include <stdio.h>
int main(void) {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n > 0) {
int digit = n % 10;
sum = sum + digit;
n = n / 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}Enter a number: 40213
Sum of digits = 10n = int(input("Enter a number: "))
total = 0
while n > 0:
total += n % 10
n //= 10
print("Sum of digits =", total)Enter a number: 40213
Sum of digits = 10Dry runs
Before running code, trace it by hand with a table of variables — the single best debugging habit:
| Step | n | digit | sum |
|---|---|---|---|
| start | 40213 | – | 0 |
| 1 | 4021 | 3 | 3 |
| 2 | 402 | 1 | 4 |
| 3 | 40 | 2 | 6 |
| 4 | 4 | 0 | 6 |
| 5 | 0 | 4 | 10 |
Test yourself
4 questions · pick an answer to check it instantly
- 1
Which flowchart symbol represents a decision?
A diamond has one entry and a Yes and a No exit. - 2
Which flowchart symbol is used for "Read marks"?
Input and output use the parallelogram. - 3
Breaking a problem into smaller sub-problems is called...
Decomposition is the first step of computational thinking. - 4
In a dry run of the digit-sum algorithm for n = 123, what is
sumafter the second pass of the loop?After the first pass sum = 3; after the second, 3 + 2 = 5.