Your first flowcharts: steps in order, then a decision
Published . By the DiagramDesk team.
Every program is built from three shapes of control: doing things one after another, choosing between paths, and repeating. This page covers the first two with the three exercises most introductory courses start with, each drawn as a flowchart and then written as pseudocode, Python and C.
Add two numbersEven or oddLargest of threeChecking your chart
The four shapes you need
Programming flowcharts use a small part of the full symbol set. A stadium marks where the program starts and ends. A parallelogram is input or output: reading a number, printing a result. A rectangle is a step that changes something, usually an assignment such as sum = a + b. A diamond is a question with a yes-or-no answer, and every line leaving it is labelled with the answer it stands for. The flowchart symbols guide has the rest, and a printable sheet of them.
Add two numbers: pure sequence
The simplest program there is: read two numbers, add them, print the answer. It has no decision and no loop, which makes it the right place to get the shapes right. The reading is one parallelogram, not two, because it is one input step; the addition is a rectangle; the printing is another parallelogram.
Pseudocode
START
READ a, b
sum ← a + b
PRINT sum
ENDPython
a = int(input())
b = int(input())
total = a + b
print(total)C
#include <stdio.h>
int main(void) {
int a, b;
scanf("%d %d", &a, &b);
int sum = a + b;
printf("%d\n", sum);
return 0;
}One detail in the Python is worth copying: the variable is called total, not sum, because sum is already the name of a function Python provides, and reusing it hides that function for the rest of the program. The C has no such problem and keeps the name the flowchart uses.
Even or odd: the first decision
A number is even when dividing it by two leaves no remainder. In the flowchart that becomes one diamond, n mod 2 = 0?, with a Yes line to "Print Even" and a No line to "Print Odd". Both paths meet again before the End, and that matters: a flowchart with two End shapes is not wrong, but one that merges back shows at a glance that exactly one of the two branches runs.
Pseudocode
START
READ n
IF n MOD 2 = 0 THEN
PRINT "Even"
ELSE
PRINT "Odd"
END IF
ENDPython
n = int(input())
if n % 2 == 0:
print("Even")
else:
print("Odd")C
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
if (n % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}The test is written as "remainder equals 0", never as "remainder equals 1", and there is a reason beyond style. In C, the remainder of a negative number is negative or zero: the C standard defines integer division as truncating towards zero, so -3 % 2 is -1. A program that checks n % 2 == 1 for odd would call −3 even. Python rounds the other way and gives 1, so the same bug hides in Python and appears in C. Testing for zero is right in both.
Largest of three: decisions in a chain
With three numbers, one question is not enough. The first diamond asks whether a is at least as big as both the others; if so, it is the largest and the work is done. If not, the answer is either b or c, and a second diamond settles which. Each path ends by setting the same variable, largest, so a single "Print largest" at the bottom serves all three.
Pseudocode
START
READ a, b, c
IF a ≥ b AND a ≥ c THEN
largest ← a
ELSE IF b ≥ c THEN
largest ← b
ELSE
largest ← c
END IF
PRINT largest
ENDPython
a = int(input())
b = int(input())
c = int(input())
if a >= b and a >= c:
largest = a
elif b >= c:
largest = b
else:
largest = c
print(largest)C
#include <stdio.h>
int main(void) {
int a, b, c, largest;
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
largest = a;
else if (b >= c)
largest = b;
else
largest = c;
printf("%d\n", largest);
return 0;
}The comparisons use "greater than or equal to", and the most common mistake in this exercise is using "greater than" instead. Try 5, 5 and 3 with strict comparisons: a is not greater than b, so the first test fails; b is not greater than a either, so a second test written the same way fails too, and the program falls through to c and prints 3. Ties are exactly where a chart like this breaks, which is why the test cases for this page include 5, 5, 3 and 7, 7, 7.
Checking a chart before you hand it in
- Follow every path from Start with real numbers, including the awkward ones: zero, a negative number, two equal values.
- Check that every diamond has a label on each line that leaves it.
- Check that every path reaches End. A path that stops in mid-air is the flowchart version of a program that crashes.
- Check that inputs and outputs are parallelograms and calculations are rectangles; examiners mark the shapes.
When decisions start repeating themselves, a loop is usually what is missing. The loops page picks up there, with factorial, the Fibonacci numbers and a prime test.