C Programming ยท August 23, 2026

Introduction to Algorithms: Thinking Before You Code

Module I ยท Algorithms & Flowcharts ยท Part 1


๐ŸŽฏ Why This Matters

So far in this course, you’ve learned the building blocks of C โ€” its structure, its naming rules, its variables. But before any of that syntax matters, every programmer has to answer a much bigger question first: what exactly should the program do, step by step, before I write a single line of code?

That’s what an algorithm is. It’s the plan. The recipe. The set of instructions that solves a problem โ€” written in plain, ordered steps, completely independent of any programming language. Learn to think in algorithms first, and writing the actual C code afterward becomes almost mechanical, because you already know exactly what needs to happen and in what order.

๐Ÿ“– What Is an Algorithm?

An algorithm is a finite, ordered sequence of well-defined steps that takes some input, processes it, and produces an output โ€” and that, if followed exactly, always solves the intended problem.

Think about a recipe for making tea:

  1. Boil water
  2. Add tea leaves
  3. Let it steep for 3 minutes
  4. Add milk and sugar
  5. Pour into a cup

That’s an algorithm. It doesn’t matter whether you make the tea in Lahartara or in London โ€” the steps are the same, and anyone who follows them correctly ends up with a cup of tea. A programming algorithm works exactly the same way: it’s language-independent. The same algorithm for “find the largest of two numbers” could later be written in C, Python, or Java โ€” the logic doesn’t change, only the syntax used to express it.

โœ… Characteristics of a Good Algorithm

CharacteristicWhat it means
FinitenessThe algorithm must terminate after a limited number of steps โ€” it can’t run forever
DefinitenessEvery step must be precise and unambiguous โ€” no room for guessing what to do next
InputIt should accept zero or more well-defined inputs
OutputIt must produce at least one well-defined output
EffectivenessEach step must be simple enough to actually be carried out โ€” no vague or impossible instructions

๐Ÿ’ก Key Idea:An algorithm is the “what to do,” while a flowchart (our next post) is the “picture of what to do,” and code is the “how to actually do it” in a specific language. All three describe the same logic in three different forms.

โœ๏ธ How We’ll Write Algorithms in This Post

Throughout this post, algorithms are written as a numbered list of steps, always beginning with Step 1: Start and ending with a Stop step. Where a decision needs to send execution somewhere other than the very next line, we write it explicitly as “Go to Step X” โ€” this is exactly how early programmers reasoned about control flow before structured loops and functions became common, and it’s still the clearest way to see why a flowchart’s arrows point where they do.

๐Ÿ’ก Why we use “Go to Step X” here: In real C code you’ll almost always use if, else, for, and while instead of literal jumps. But at the algorithm level โ€” before any syntax exists โ€” writing out explicit jumps forces you to see precisely which condition sends control where. This is also exactly how a flowchart’s arrows will look once we draw them in the next post, so it’s the perfect stepping stone.

1๏ธโƒฃ2๏ธโƒฃ Twelve Algorithms, From Simple to Branching

We’ll now work through twelve algorithms, increasing in complexity โ€” starting with two plain sequential algorithms, moving into single and multi-way decisions, then loops, and finishing with a menu-driven program and one last, simple loop that brings the pattern full circle.


1Area of a Rectangle

Problem: Take the length and width of a rectangle as input and calculate its area.

Real-world hook: This is exactly what a flooring contractor does before quoting a price โ€” measure the length and width of a room, multiply them, and that’s the number of tiles needed.

Step 1: Start
Step 2: Input Length and Width
Step 3: Compute Area = Length ร— Width
Step 4: Print Area
Step 5: Stop

๐Ÿ–ฅ๏ธ Dry run: Length = 12, Width = 8 โ†’ Area = 12 ร— 8 = 96 โ†’ Output: 96

What to notice: Just like the addition algorithm right after it, this is purely sequential โ€” five steps, each running exactly once, top to bottom, with no decisions and no repetition anywhere. Two sequential algorithms in a row like this make the pattern unmistakable before we introduce our first branch in Algorithm 3: input, process, output โ€” nothing more.


2Add Two Numbers

Problem: Take two numbers as input and print their sum.

Real-world hook: This is exactly what happens when a shopkeeper adds up the price of two items on a bill โ€” read both values, add them, write down the total.

Step 1: Start
Step 2: Input two numbers, A and B
Step 3: Compute Sum = A + B
Step 4: Print Sum
Step 5: Stop

๐Ÿ–ฅ๏ธ Dry run: A = 5, B = 3 โ†’ Sum = 8 โ†’ Output: 8

What to notice: This algorithm is purely sequential โ€” every step runs exactly once, in order, with no decisions and no repetition. It’s the simplest possible shape an algorithm can take, and it’s the baseline every other algorithm below builds on.


3Largest of Two Numbers

Problem: Take two numbers and determine which one is larger.

Real-world hook: Choosing the taller of two friends standing next to each other โ€” you compare, then declare one of them “taller,” never both.

Step 1: Start
Step 2: Input two numbers, A and B
Step 3: If A > B, go to Step 4; otherwise go to Step 5
Step 4: Print "A is the largest"; go to Step 6
Step 5: Print "B is the largest"
Step 6: Stop

๐Ÿ–ฅ๏ธ Dry run: A = 10, B = 7 โ†’ Step 3 is true โ†’ Step 4 runs โ†’ Output: A is the largest
๐Ÿ–ฅ๏ธ Dry run: A = 4, B = 9 โ†’ Step 3 is false โ†’ Step 5 runs โ†’ Output: B is the largest

What to notice: This is our first decision (branching) algorithm. Step 3 is the first point where execution can go one of two different directions โ€” and once one branch finishes, it jumps straight to the Stop step, skipping the other branch entirely.


4Largest of Three Numbers

Problem: Take three numbers and determine the largest among them.

Real-world hook: A judge comparing three contestants’ scores โ€” they can’t just compare two at a time and stop; every entrant needs to be checked against the current leader.

Step 1: Start
Step 2: Input three numbers, A, B, C
Step 3: If A > B, go to Step 4; otherwise go to Step 6
Step 4: If A > C, go to Step 5; otherwise go to Step 8
Step 5: Print "A is the largest"; go to Step 9
Step 6: If B > C, go to Step 7; otherwise go to Step 8
Step 7: Print "B is the largest"; go to Step 9
Step 8: Print "C is the largest"
Step 9: Stop

๐Ÿ–ฅ๏ธ Dry run: A = 3, B = 8, C = 5
Step 3: is 3 > 8? No โ†’ go to Step 6
Step 6: is 8 > 5? Yes โ†’ go to Step 7
Step 7: Print “B is the largest” โ†’ go to Step 9 โ†’ Stop
Output: B is the largest โœ… (8 is indeed the largest)

What to notice: This is a nested decision โ€” a decision inside a decision. Notice how each branch only ever needs one more comparison to reach a final answer, never comparing all three numbers against each other redundantly. This kind of efficient branching is exactly what you’ll later see as multiple diamond shapes in a flowchart.


5Odd or Even Checker

Problem: Determine whether a given number is odd or even.

Real-world hook: Splitting a group of people into two equal teams โ€” if anyone is left over after pairing everyone up, the group size was odd.

Step 1: Start
Step 2: Input a number, N
Step 3: Compute R = N mod 2 (the remainder after dividing by 2)
Step 4: If R = 0, go to Step 5; otherwise go to Step 6
Step 5: Print "Even"; go to Step 7
Step 6: Print "Odd"
Step 7: Stop

๐Ÿ–ฅ๏ธ Dry run: N = 17 โ†’ R = 17 mod 2 = 1 โ†’ Step 4 is false โ†’ Step 6 runs โ†’ Output: Odd

What to notice: The mod (modulo) operation โ€” which gives the remainder of a division โ€” is the standard trick for testing divisibility in algorithms. You’ll use this same idea again in the prime number checker later in this post.


6Positive, Negative, or Zero Checker

Problem: Given a number, determine whether it is positive, negative, or exactly zero โ€” a three-way decision instead of a two-way one.

Real-world hook: Checking a bank balance: it can be in credit (positive), in overdraft (negative), or exactly zero โ€” three distinct outcomes, not two.

Step 1: Start
Step 2: Input a number, N
Step 3: If N > 0, go to Step 6
Step 4: If N < 0, go to Step 7
Step 5: Print "Zero"; go to Step 8
Step 6: Print "Positive"; go to Step 8
Step 7: Print "Negative"
Step 8: Stop

๐Ÿ–ฅ๏ธ Dry run: N = 0
Step 3: is 0 > 0? No โ†’ fall through to Step 4
Step 4: is 0 < 0? No โ†’ fall through to Step 5
Step 5: Print “Zero” โ†’ Step 8 โ†’ Stop

What to notice: This algorithm has three possible outcomes, not two โ€” and Step 5 is reached simply by both earlier conditions failing, without needing its own explicit condition. This “falling through” when nothing else matched is a common and efficient pattern: you don’t always need a condition for every single branch, just for all-but-one.


7Leap Year Checker

Problem: Determine whether a given year is a leap year, using the real calendar rule: a year is a leap year if it’s divisible by 4, except century years, which must be divisible by 400.

Real-world hook: This is the exact rule your school calendar and every digital calendar app silently applies every time it decides whether February has 28 or 29 days.

Step 1: Start
Step 2: Input a year, Y
Step 3: If Y mod 4 = 0, go to Step 4; otherwise go to Step 7
Step 4: If Y mod 100 = 0, go to Step 5; otherwise go to Step 6
Step 5: If Y mod 400 = 0, go to Step 6; otherwise go to Step 7
Step 6: Print "Leap Year"; go to Step 8
Step 7: Print "Not a Leap Year"
Step 8: Stop

๐Ÿ–ฅ๏ธ Dry run: Y = 1900
Step 3: 1900 mod 4 = 0 โ†’ true โ†’ go to Step 4
Step 4: 1900 mod 100 = 0 โ†’ true โ†’ go to Step 5
Step 5: 1900 mod 400 = 300, not 0 โ†’ false โ†’ go to Step 7
Output: Not a Leap Year โœ… (matches the real calendar โ€” 1900 was not a leap year)

๐Ÿ–ฅ๏ธ Dry run: Y = 2000 โ†’ Step 3 true โ†’ Step 4 true โ†’ Step 5: 2000 mod 400 = 0 โ†’ true โ†’ go to Step 6 โ†’ Output: Leap Year โœ…

What to notice: This algorithm uses compound, nested conditions to correctly encode a rule that isn’t a simple single check. It’s a great example of why real-world rules often need several linked decisions rather than just one if โ€” and why tracing through a couple of tricky test cases (like century years) is essential before trusting an algorithm is correct.


8Sum of First N Natural Numbers

Problem: Given a number N, calculate the sum 1 + 2 + 3 + ... + N.

Real-world hook: A cashier stacking coins one at a time and keeping a running total โ€” the same “add one, then repeat” action happens over and over until there are no coins left.

Step 1: Start
Step 2: Input N
Step 3: Set Sum = 0 and Counter = 1
Step 4: If Counter > N, go to Step 7
Step 5: Set Sum = Sum + Counter and Counter = Counter + 1
Step 6: Go to Step 4
Step 7: Print Sum
Step 8: Stop

๐Ÿ–ฅ๏ธ Dry run: N = 4
Counter=1: 1>4? No โ†’ Sum=0+1=1, Counter=2 โ†’ back to Step 4
Counter=2: 2>4? No โ†’ Sum=1+2=3, Counter=3 โ†’ back to Step 4
Counter=3: 3>4? No โ†’ Sum=3+3=6, Counter=4 โ†’ back to Step 4
Counter=4: 4>4? No โ†’ Sum=6+4=10, Counter=5 โ†’ back to Step 4
Counter=5: 5>4? Yes โ†’ go to Step 7 โ†’ Output: 10

What to notice: Step 6’s jump back to Step 4 is what creates a loop โ€” the exact same block of steps runs repeatedly until the condition in Step 4 finally becomes true. This “jump backward” is the single most important pattern in this entire post: every loop you’ll ever write in C (for, while) is really just this same backward jump, wrapped in friendlier syntax.


9Factorial of a Number

Problem: Given a number N, calculate N! (N factorial) โ€” the product 1 ร— 2 ร— 3 ร— ... ร— N.

Real-world hook: Counting the number of different ways to arrange a shelf of books โ€” factorial growth is exactly why arranging even 5 books already has 120 possible orders.

Step 1: Start
Step 2: Input N
Step 3: Set Fact = 1 and Counter = 1
Step 4: If Counter > N, go to Step 7
Step 5: Set Fact = Fact ร— Counter and Counter = Counter + 1
Step 6: Go to Step 4
Step 7: Print Fact
Step 8: Stop

๐Ÿ–ฅ๏ธ Dry run: N = 4
Counter=1: Fact=1ร—1=1, Counter=2
Counter=2: Fact=1ร—2=2, Counter=3
Counter=3: Fact=2ร—3=6, Counter=4
Counter=4: Fact=6ร—4=24, Counter=5
Counter=5: 5>4? Yes โ†’ Output: 24 โœ… (4! = 24)

What to notice: Structurally this is almost identical to the sum algorithm above โ€” same loop shape, same jump-back pattern โ€” just with multiplication instead of addition, and Fact starting at 1 instead of 0 (since multiplying by 0 would wipe out the whole result). Recognizing this kind of reusable pattern is a core algorithmic thinking skill.


10Prime Number Checker

Problem: Determine whether a given number is prime (divisible only by 1 and itself).

Real-world hook: Checking whether a group of people can be split evenly into smaller equal teams with no leftovers โ€” if you try every possible team size and none works except 1 and the full group, the number is prime.

Step 1: Start
Step 2: Input N
Step 3: If N โ‰ค 1, go to Step 10 (not prime by definition)
Step 4: Set Counter = 2
Step 5: If Counter = N, go to Step 9 (no divisor was ever found โ€” it's prime)
Step 6: If N mod Counter = 0, go to Step 10 (a divisor was found โ€” exit the loop early)
Step 7: Set Counter = Counter + 1
Step 8: Go to Step 5
Step 9: Print "Prime"; go to Step 11
Step 10: Print "Not Prime"
Step 11: Stop

๐Ÿ–ฅ๏ธ Dry run: N = 8 (should NOT be prime)
Counter=2: is 2=8? No โ†’ is 8 mod 2 = 0? Yes โ†’ immediately go to Step 10
Output: Not Prime โ€” notice the loop exited after just one check, without wasting time testing counter = 3, 4, 5, 6, 7

๐Ÿ–ฅ๏ธ Dry run: N = 7 (should be prime)
Counter runs 2, 3, 4, 5, 6 โ€” none divide evenly โ€” Counter finally reaches 7, which equals N โ†’ go to Step 9 โ†’ Output: Prime

What to notice: Step 6 is exactly the “early exit jump” pattern you asked about โ€” the moment a divisor is found, the algorithm doesn’t bother checking any remaining numbers; it jumps straight out of the loop to print the answer. This is a huge efficiency idea: stop checking the moment you already know the answer, instead of blindly finishing every step regardless.


11Menu-Driven Simple Calculator

Problem: Build a calculator that asks the user to pick an operation from a menu, then jumps to the correct block of steps for that operation โ€” and safely handles the one input that could crash it (dividing by zero).

Real-world hook: This is exactly how an ATM works โ€” you pick “Withdraw,” “Deposit,” or “Check Balance” from a menu, and the machine jumps straight to the specific set of steps for the option you picked, completely skipping the steps for the other two.

Step 1: Start
Step 2: Display menu: 1-Add, 2-Subtract, 3-Multiply, 4-Divide
Step 3: Input Choice
Step 4: Input two numbers, A and B
Step 5: If Choice = 1, go to Step 9
Step 6: If Choice = 2, go to Step 10
Step 7: If Choice = 3, go to Step 11
Step 8: If Choice = 4, go to Step 12; otherwise go to Step 14
Step 9: Set Result = A + B; go to Step 13
Step 10: Set Result = A โˆ’ B; go to Step 13
Step 11: Set Result = A ร— B; go to Step 13
Step 12: If B = 0, go to Step 15; otherwise set Result = A รท B; go to Step 13
Step 13: Print Result; go to Step 16
Step 14: Print "Invalid Choice"; go to Step 16
Step 15: Print "Error: Division by zero"
Step 16: Stop

๐Ÿ–ฅ๏ธ Dry run: Choice = 4, A = 10, B = 0
Step 5: 4=1? No. Step 6: 4=2? No. Step 7: 4=3? No. Step 8: 4=4? Yes โ†’ go to Step 12
Step 12: is B = 0? Yes โ†’ go to Step 15 โ†’ Output: Error: Division by zero
(Notice the calculator never even attempts the unsafe division โ€” it catches the problem and jumps to a dedicated error step instead)

๐Ÿ–ฅ๏ธ Dry run: Choice = 3, A = 6, B = 5 โ†’ Step 5,6 false, Step 7: 3=3? Yes โ†’ go to Step 11 โ†’ Result = 30 โ†’ Step 13 โ†’ Output: 30

What to notice: This algorithm ties everything together โ€” it’s a multi-way jump (like a menu of doors, only one of which opens) combined with a safety check that redirects execution to an error step instead of letting the program crash. Every one of the four operation blocks is completely independent โ€” the algorithm jumps into exactly one of them and skips the rest entirely, which is exactly how a switch statement in C behaves once you learn it later in this course.


12Print the Multiplication Table of a Number

Problem: Given a number, print its multiplication table from 1 to 10 (e.g., for 5: 5 ร— 1 = 5, 5 ร— 2 = 10, and so on up to 5 ร— 10 = 50).

Real-world hook: This is exactly the table every student memorizes by heart in primary school โ€” a fixed number, multiplied by 1, then 2, then 3, all the way up to 10, one line at a time.

Step 1: Start
Step 2: Input a number, N
Step 3: Set Counter = 1
Step 4: If Counter > 10, go to Step 7
Step 5: Print N ร— Counter
Step 6: Set Counter = Counter + 1; go to Step 4
Step 7: Stop

๐Ÿ–ฅ๏ธ Dry run: N = 5
Counter=1: 1>10? No โ†’ Print “5 ร— 1 = 5” โ†’ Counter=2 โ†’ back to Step 4
Counter=2: 2>10? No โ†’ Print “5 ร— 2 = 10” โ†’ Counter=3 โ†’ back to Step 4
โ€ฆ this repeats โ€ฆ
Counter=10: 10>10? No โ†’ Print “5 ร— 10 = 50” โ†’ Counter=11 โ†’ back to Step 4
Counter=11: 11>10? Yes โ†’ go to Step 7 โ†’ Stop

What to notice: This is the simplest possible loop in the whole post โ€” there’s no arithmetic trick like mod or division involved, just a counter that climbs from 1 to 10, printing one line and jumping back to Step 4 each time, until the condition in Step 4 finally stops it. It’s the perfect algorithm to close on, because it strips the loop pattern down to its bare essentials: repeat a simple action a fixed number of times โ€” the same shape you’ll meet again and again as a for loop once we get to writing this in actual C code.


๐Ÿ“Š Comparing All Twelve: What Shape of Algorithm Is Each One?

#AlgorithmStructure typeKey idea demonstrated
1Area of a RectangleSequentialSteps run once, in order, no decisions
2Add Two NumbersSequentialA second pure input โ†’ process โ†’ output example
3Largest of TwoSingle branchOne condition, two possible paths
4Largest of ThreeNested branchA decision inside a decision
5Odd or EvenSingle branchUsing mod to test a property
6Positive/Negative/ZeroMulti-way branchThree outcomes, with “fall-through”
7Leap YearCompound nested branchReal-world rules often need linked conditions
8Sum of N NumbersLoopJumping backward to repeat steps
9FactorialLoopReusing the same loop pattern for a new purpose
10Prime CheckerLoop + early exitJumping out of a loop the moment the answer is known
11Menu CalculatorMulti-way jump + safety checkChoosing one path out of several, and guarding against errors
12Multiplication TableLoopThe simplest loop shape โ€” repeat a fixed action a set number of times

๐Ÿ’ก The big pattern: Notice that every algorithm you’ll ever write is built from just three ingredients: sequence (do this, then that), selection (choose a path based on a condition), and iteration (repeat a block until a condition is met). Programmers call this the Three Constructs of Structured Programming โ€” and all twelve programs above are simply different combinations of these three ideas.

๐ŸŽ“ Practice Exercises

Exercise 1: Trace It

Using Algorithm 9 (Prime Checker), trace through N = 13 step by step and write down the value of Counter at each pass, until you reach the final output.

Exercise 2: Modify It

Algorithm 5 (Positive/Negative/Zero) currently only prints a message. Modify the algorithm so that if the number is negative, it also prints the number’s absolute value.

Exercise 3: Design Your Own

Write a numbered-step algorithm (with explicit “Go to Step X” jumps) to check whether a given number is a multiple of both 3 and 5. Try to reuse the branching style from Algorithm 6.

โ“ Frequently Asked Questions

Q: Is an algorithm the same thing as a program?
No. An algorithm is the language-independent plan โ€” the logic. A program is that plan translated into the exact syntax of a specific language, like C. The same algorithm can become many different programs.

Q: Why do some of these algorithms use “Go to Step X” instead of just writing “if…else”?
Writing out explicit jumps at the algorithm stage makes the underlying control flow completely visible โ€” which step leads to which โ€” and it’s the most direct bridge to drawing a flowchart, where every jump becomes an arrow.

Q: Do real C programs actually use “goto” like this?
Rarely, and it’s generally discouraged in modern C โ€” structured constructs like if, else, for, and while achieve the same jumps far more safely and readably. We use explicit jumps here purely as a teaching tool to expose the logic underneath those constructs.

Q: What’s the difference between a loop and an early exit?
A loop repeats a block of steps until a condition is met (see Algorithms 7 and 8). An early exit is a jump that breaks out of that loop before it would naturally finish, because the answer is already known โ€” exactly what happens in Algorithm 9 the moment a divisor is found.

โœ… Key Takeaways

  • An algorithm is a finite, ordered, unambiguous set of steps that solves a problem โ€” completely independent of any programming language.
  • Every algorithm, no matter how complex, is built from just three constructs: sequence, selection (branching), and iteration (looping).
  • A “Go to Step X” jump is how an algorithm expresses a decision, a loop, or an early exit โ€” and every one of these jumps will become an arrow once we draw a flowchart.
  • Jumping backward to an earlier step creates a loop; jumping forward past remaining steps creates an early exit or skips an irrelevant branch entirely.
  • Good algorithms include safety checks (like the divide-by-zero guard in Algorithm 10) โ€” anticipating what could go wrong is as much a part of algorithmic thinking as solving the main problem.

๐Ÿš€ Next up: Now that you can read and write algorithms confidently โ€” including branches, loops, and jumps โ€” we’ll take every one of these same twelve problems and turn them into flowcharts: the visual diagrams that turn each “Go to Step X” into an arrow, and each decision into a diamond shape you can literally follow with your finger.