📋 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:
🗺️ 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).
i controls how many times we repeat. This algorithm has one sequence (init → read → add → print) wrapped in one loop (repeat N times).
nsum = 0, i = 1numsum = sum + numi = i + 1i ≤ n, go to Step 4sum#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; }
How many numbers? 4 Enter number 1: 10 Enter number 2: 25 Enter number 3: 8 Enter number 4: 17 Sum = 60
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.nn mod 2 = 0 then go to Step 4, else go to Step 5n is Even". Go to Step 6n is Odd"#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 */ }
Enter a number: 14 14 is Even Enter a number: 7 7 is Odd
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.
A, B, CA > B then go to Step 4, else go to Step 6A > C then go to Step 5, else go to Step 7B > C then print "B is largest", else print "C is largest". Go to Step 8#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; }
Enter three numbers: 15 42 28 B = 42 is largest Enter three numbers: 99 50 70 A = 99 is largest
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.
nn < 0 print "Invalid input" and go to Step 8fact = 1, i = 1i > n go to Step 7fact = fact × i. Set i = i + 1. Go to Step 5n! = fact#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; }
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
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.
- 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.