Brute force (exhaustive search) generates every candidate solution and tests each one. It is the simplest to write and reason about, always correct when implemented properly, and often the right choice for small inputs or as a reference to test faster algorithms. Its cost grows quickly: pairs are O(n^2), subsets O(2^n), permutations O(n!).
Remember
for each candidate:
if candidate is valid: use it
Reference
Cheat Sheet
Keep these facts handy while you work with Brute Force Algorithm in C in C.
#include <stdio.h>
int main(void) {
int a[] = {2, 7, 11, 15};
int n = 4, target = 26, checks = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
checks++;
if (a[i] + a[j] == target) {
printf("pair: %d + %d = %d (checked %d pairs)\n", a[i], a[j], target, checks);
return 0;
}
}
}
printf("no pair found after %d checks\n", checks);
return 0;
}
Output
pair: 11 + 15 = 26 (checked 6 pairs)
How It Works
Pairs are tried in the order (2,7), (2,11), (2,15), (7,11), (7,15), (11,15).
Only the last pair matches, so all six are checked.
For n items the loop checks n(n-1)/2 pairs in the worst case.
Edge Cases & Pitfalls
Check these before relying on Brute Force Algorithm in C in real programs.
scale
Ignoring growth
A solution that checks all subsets is unusable beyond about 25 items.
early
Not stopping early
Return as soon as you find a valid answer if only one is needed.
Remember
Key Takeaways
Try everything.
Simple and correct.
Great as a baseline.
One line: Brute force (exhaustive search) generates every candidate solution and tests each one.
Frequently Asked Questions
An approach that systematically tries all possible candidates and checks each one for validity.
When the input is small, when you need a simple correct reference, or when no better algorithm is known.
Continue with "Divide and Conquer in C" next. Split a problem into independent halves and combine the results. The C Algorithm Techniques hub lists every topic in order.