An algorithm takes input, follows finite, unambiguous steps, and produces output. It must always terminate and each step must be doable. The same algorithm can be written in any language; the C code is just one implementation. Euclid's algorithm for the greatest common divisor is over two thousand years old and a perfect first example.
Remember
/* gcd(a, b):
while b != 0:
r = a mod b
a = b
b = r
return a */
Reference
Cheat Sheet
Keep these facts handy while you work with What Is an Algorithm in C? in C.
#include <stdio.h>
int main(void) {
int a = 48, b = 18;
while (b != 0) {
int r = a % b;
printf("%d mod %d = %d\n", a, b, r);
a = b;
b = r;
}
printf("gcd = %d\n", a);
return 0;
}
Output
48 mod 18 = 12
18 mod 12 = 6
12 mod 6 = 0
gcd = 6
How It Works
Each round replaces (a, b) with (b, a mod b).
The remainder shrinks every round, so the loop must end.
When the remainder is 0, the previous divisor, 6, is the gcd.
Edge Cases & Pitfalls
Check these before relying on What Is an Algorithm in C? in real programs.
loop
No termination argument
If nothing shrinks each iteration, the loop may never end.
edge
Ignoring edge cases
What if b starts at 0, or the inputs are negative? Decide and document.
Remember
Key Takeaways
Input, output, finite, definite.
Always prove it ends.
Language independent.
One line: An algorithm takes input, follows finite, unambiguous steps, and produces output.
Frequently Asked Questions
Input, output, finiteness, definiteness, and effectiveness: every step is clear and executable.
Not necessarily. A program that loops forever does not satisfy finiteness.
Continue with "Writing Pseudocode for C Programs" next. Describe the steps in plain structured text before coding. The C Algorithm Basics hub lists every topic in order.