Algorithms & Flowcharts in C — 4 Examples
0%
C Fundamentals  ·  Problem Solving

Algorithms &
Flowcharts in C

Learn how to write step-by-step algorithms in plain English, draw flowcharts using standard symbols, then translate both into working C code — four complete examples from simple to nested logic.

1
Sum of N Numbers
2
Even / Odd Check
3
Largest of Three
4
Factorial

📋 What is an Algorithm?

An algorithm is a finite, ordered set of well-defined steps that solves a problem or accomplishes a task. Every program you write is the implementation of an algorithm. Before writing a single line of C code, writing the algorithm in plain language helps you think clearly about the logic without worrying about syntax.

A good algorithm has five properties:

1
Input — zero or more values are supplied to the algorithm (e.g., a number, an array).
2
Output — at least one result is produced (e.g., a sum, a Yes/No answer).
3
Definiteness — every step is clear and unambiguous. "Do something useful" is not a valid step.
4
Finiteness — the algorithm must terminate after a finite number of steps. It cannot run forever.
5
Effectiveness — every step must be basic enough to be carried out exactly (by hand or by a computer).

🗺️ What is a Flowchart?

A flowchart is the pictorial (visual) representation of an algorithm. It uses standard shapes connected by arrows to show the flow of control. Flowcharts make it easy to spot logic errors, missing conditions, and infinite loops before writing any code.

The workflow is always: Problem → Algorithm (words) → Flowchart (picture) → C Code (program).

START/END
Oval / Terminator
Start or End of the algorithm
a = b + c
Rectangle / Process
Calculation, assignment, any action step
a > b ?
Diamond / Decision
Yes/No or True/False branch point
Input/Output
Parallelogram
Read input from user or display output
Sub-process
Double-bar Rect
Predefined process / function call
A
Circle / Connector
Connects two parts of a flowchart on different areas
How to write an algorithm — the golden template: Start with "Step 1: START". Then list inputs ("Step 2: Read…"), computations ("Step 3: Set sum = …"), conditions ("Step 4: If x > y then…"), loops ("Step 5: Repeat steps 3–4 while…"), outputs ("Step 6: Print…"), and finally "Step N: STOP". Number every step. Keep each step to one action. Avoid C syntax — write in plain English or pseudocode.
example 1 — sum of n numbers
1
➕ Sum of N Numbers — Loop Algorithm
Read N numbers one by one, add each to a running total, print the sum — the simplest loop pattern
Sequential + Loop
The accumulator pattern is the most fundamental algorithm in programming. We initialise a sum variable to zero, then repeatedly read a number and add it to the sum until we have processed all N numbers. The loop counter i controls how many times we repeat. This algorithm has one sequence (init → read → add → print) wrapped in one loop (repeat N times).
📋 Algorithm — Sum of N Numbers
1.
START
2.
Read the value of n
3.
Set sum = 0, i = 1
4.
Read next number into num
5.
Set sum = sum + num
6.
Set i = i + 1
7.
If i ≤ n, go to Step 4
8.
Print sum
9.
STOP
🗺️ Flowchart — Sum of N Numbers
START Read n sum=0, i=1 Read num sum = sum + num i = i + 1 i ≤ n ? YES NO Print sum STOP
sum_n.c
C
#include <stdio.h>

int main() {
    int n, i, num, sum = 0;

    printf("How many numbers? ");
    scanf("%d", &n);                    /* Step 2: Read n   */

    /* Step 3: i=1 already done by loop init */
    for (i = 1; i <= n; i++) {           /* Steps 7: loop control */
        printf("Enter number %d: ", i);
        scanf("%d", &num);               /* Step 4: Read num */
        sum = sum + num;                  /* Step 5: accumulate */
    }                                     /* Step 6: i++ is for(;;i++) */

    printf("Sum = %d\n", sum);            /* Step 8: Print    */
    return 0;
}
output
How many numbers? 4
Enter number 1: 10
Enter number 2: 25
Enter number 3: 8
Enter number 4: 17
Sum = 60
One algorithm step = one C statement. Notice how Step 4 (Read num) maps exactly to scanf(…, &num), Step 5 maps to sum = sum + num, and the loop condition in Step 7 maps to i <= n. When you write a clear algorithm first, translating to C becomes mechanical.
example 2 — even / odd check
2
⚖️ Even or Odd — Decision Branch Algorithm
One decision diamond splits flow into two paths — the simplest if/else pattern every flowchart must demonstrate
Decision / Branch
A decision is a point where the algorithm asks a question and takes one of two (or more) paths depending on the answer. In a flowchart, a diamond represents this. The YES/NO arrows lead to different process boxes. After both branches complete their work they rejoin — called a merge point. This example checks if a number is even or odd using the modulo operator.
📋 Algorithm — Even or Odd
1.
START
2.
Read integer n
3.
If n mod 2 = 0 then go to Step 4, else go to Step 5
4.
Print "n is Even". Go to Step 6
5.
Print "n is Odd"
6.
STOP
🗺️ Flowchart — Even or Odd
START Read n n%2==0? Print "Even" YES Print "Odd" NO STOP
even_odd.c
C
#include <stdio.h>

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);                  /* Step 2: Read n         */

    if (n % 2 == 0)                   /* Step 3: Decision n%2=0 */
        printf("%d is Even\n", n);    /* Step 4: YES branch     */
    else
        printf("%d is Odd\n", n);     /* Step 5: NO branch      */

    return 0;                          /* Step 6: STOP           */
}
output
Enter a number: 14
14 is Even

Enter a number: 7
7 is Odd
The merge point is implicit in C. After the if/else block, both branches converge and execution continues at the next statement — this is the merge you see in the flowchart. In a flowchart you draw the merge explicitly with converging arrows; in C it is automatic.
example 3 — largest of three numbers
3
🏆 Largest of Three — Nested Decision Algorithm
Two chained diamond decisions find the maximum — shows how nested if-else maps to a nested flowchart
Nested Decision
Nested decisions occur when one branch of a diamond leads to another diamond. This is how multiple conditions are handled. To find the largest of three numbers, we first compare A and B. If A is larger, we compare A with C. Otherwise we compare B with C. Two chained diamonds produce three possible output paths — one for each number being the largest.
📋 Algorithm — Largest of Three
1.
START
2.
Read three numbers A, B, C
3.
If A > B then go to Step 4, else go to Step 6
4.
If A > C then go to Step 5, else go to Step 7
5.
Print "A is largest". Go to Step 8
6.
If B > C then print "B is largest", else print "C is largest". Go to Step 8
7.
Print "C is largest"
8.
STOP
🗺️ Flowchart — Largest of Three
START Read A, B, C A > B ? YES A > C ? A is largest YES C is largest NO NO B > C ? B is largest YES C is largest NO STOP
largest_three.c
C
#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);     /* Step 2 */

    if (a > b) {                          /* Step 3: A > B? */
        if (a > c)                        /* Step 4: A > C? */
            printf("A = %d is largest\n", a);  /* Step 5 */
        else
            printf("C = %d is largest\n", c);  /* Step 7 */
    } else {                               /* Step 6 */
        if (b > c)
            printf("B = %d is largest\n", b);
        else
            printf("C = %d is largest\n", c);
    }
    return 0;
}
output
Enter three numbers: 15 42 28
B = 42 is largest

Enter three numbers: 99 50 70
A = 99 is largest
Each nested decision is a new diamond in the flowchart. A common mistake is drawing one diamond with three exits. Standard flowchart diamonds have exactly two exits — YES and NO. For three numbers you always need two diamonds. For N-way decisions, use N-1 diamonds chained together, or show them as a separate decision table.
example 4 — factorial using a loop
4
🔢 Factorial — Loop with Accumulator Product
Multiply running product by each integer from 1 to N — shows how the accumulator pattern applies to multiplication
Loop + Product
Factorial (n!) means 1 × 2 × 3 × … × n. The algorithm is almost identical to the sum example — but instead of initialising an accumulator to 0 and adding, we initialise it to 1 and multiply. The loop runs from 1 to n, multiplying the current counter into fact each iteration. This example also adds an input validation step (n must be ≥ 0), showing how a pre-check decision appears in an algorithm and flowchart.
📋 Algorithm — Factorial of N
1.
START
2.
Read non-negative integer n
3.
If n < 0 print "Invalid input" and go to Step 8
4.
Set fact = 1, i = 1
5.
If i > n go to Step 7
6.
Set fact = fact × i. Set i = i + 1. Go to Step 5
7.
Print n! = fact
8.
STOP
🗺️ Flowchart — Factorial
START Read n n < 0 ? Invalid YES NO fact=1, i=1 i > n ? YES NO fact = fact × i i = i + 1 Print fact STOP
factorial.c
C
#include <stdio.h>

int main() {
    int  n, i;
    long fact = 1;

    printf("Enter n (non-negative): ");
    scanf("%d", &n);                  /* Step 2: Read n */

    if (n < 0) {                       /* Step 3: validate */
        printf("Invalid input — n must be >= 0\n");
        return 1;
    }

    /* Steps 4–6: fact=1, loop i=1..n, multiply */
    for (i = 1; i <= n; i++)
        fact *= i;                    /* fact = fact × i */

    printf("%d! = %ld\n", n, fact);   /* Step 7: print */
    return 0;
}
output
Enter n (non-negative): 6
6! = 720

Enter n (non-negative): 0
0! = 1

Enter n (non-negative): -3
Invalid input — n must be >= 0
0! = 1 is handled automatically. When n = 0, the loop condition i <= n is false immediately, so the loop body never runs and fact stays at its initial value of 1. This is correct — 0! is defined as 1 in mathematics. The algorithm does not need a special case for n = 0.
trace of factorial(5) — fact multiplied by i each iteration
Before loop
fact=1
i=1
← n=5
i=1: fact×1
fact=1
i=2
i=2: fact×2
fact=2
i=3
i=3: fact×3
fact=6
i=4
i=4: fact×4
fact=24
i=5
i=5: fact×5
fact=120
i=6
i=6 > n=5
EXIT loop
Print 120
checklist
  • An algorithm is a finite ordered set of steps with clear Input, Output, Definiteness, Finiteness, and Effectiveness. Write it in plain English — no C syntax. Number every step. One action per step.
  • Flowchart symbols: Oval = START/STOP, Rectangle = Process/Calculation, Diamond = Decision (YES/NO), Parallelogram = Input/Output, Circle = Connector. Every diamond has exactly two exits.
  • The workflow is always: Problem → Algorithm (words) → Flowchart (picture) → C Code. Each algorithm step maps directly to one or a few C statements with a comment reference.
  • Loop pattern: initialise accumulator, draw the loop-back arrow from the process box back up to the decision diamond. The YES exit of the decision continues the loop; the NO exit (or vice versa) exits to the output step.
  • Nested decisions = chained diamonds. For largest-of-three you need two diamonds. For N-way logic use N-1 chained diamonds. Both YES and NO branches must eventually merge before STOP.
  • Input validation appears as a decision diamond immediately after the Read step — check the condition, take the error path to a Print-error box and then straight to STOP; the valid path continues down to the main logic.