Bytebook Compiler

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.

4 min read2 runnable examples4-question quiz

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

  1. Start.
  2. Read A, B, C.
  3. If A ≥ B and A ≥ C, then LARGEST = A.
  4. Otherwise, if B ≥ C, then LARGEST = B.
  5. Otherwise, LARGEST = C.
  6. Print LARGEST.
  7. 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
StartRead a number nn % 2 == 0 ?YesPrint "Even"NoPrint "Odd"Stop
Flowchart for checking whether a number is even or odd.

Loops appear as an arrow going back to an earlier step:

Startsum = 0, i = 1i <= n ?Yessum = sum + i, then i = i + 1NoPrint sumStop
Flowchart for the sum 1 + 2 + … + n. The "Yes" branch loops back to the condition.

Pseudocode

Pseudocode writes an algorithm in structured, code-like English — precise like code, but free of any one language's syntax.

Pseudocode
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
END

Common 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:

C
#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;
}
Output
Enter a number: 40213
Sum of digits = 10
Sample input is includedTry it Yourself
Python
n = int(input("Enter a number: "))
total = 0
while n > 0:
    total += n % 10
    n //= 10
print("Sum of digits =", total)
Output
Enter a number: 40213
Sum of digits = 10
Sample input is includedTry it Yourself

Dry 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. 1

    Which flowchart symbol represents a decision?

  2. 2

    Which flowchart symbol is used for "Read marks"?

  3. 3

    Breaking a problem into smaller sub-problems is called...

  4. 4

    In a dry run of the digit-sum algorithm for n = 123, what is sum after the second pass of the loop?