50 Important C Programs
Why practice programs?
You cannot learn to swim by reading about water, and you cannot learn C without writing programs. This is a curated list of 50 important C programs grouped by topic, with several fully worked examples. Try each one yourself first, then check your logic.
Every program here uses only the basics covered elsewhere on the site, so each links naturally to a lesson when you want more depth.
Number programs
- Check even or odd
- Check prime number
- Factorial of a number
- Fibonacci series
- Reverse a number
- Sum of digits
- Check palindrome number
- Check Armstrong number
- Largest of three numbers
- Multiplication table
- Convert decimal to binary
- GCD and LCM of two numbers
Pattern programs
- Right-angled star triangle
- Inverted star triangle
- Pyramid of stars
- Number triangle (1, 12, 123...)
- Floyd's triangle
- Pascal's triangle
- Diamond pattern
- Hollow square pattern
Array and string programs
- Largest and smallest in an array
- Sum and average of an array
- Reverse an array
- Sort an array (bubble/selection/insertion)
- Search an element (linear/binary)
- Matrix multiplication
- Transpose of a matrix
- String length without strlen
- Reverse a string
- Check string palindrome
- Count vowels and consonants
- Convert case (upper/lower)
...and more covering linked lists, stacks, queues, file handling and small mini projects — 50 in total.
Example: prime number
#include <stdio.h>
int main() {
int n, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 2) isPrime = 0;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) { isPrime = 0; break; }
printf(isPrime ? "Prime\n" : "Not prime\n");
return 0;
}Output:
Prime
Example: reverse a number
#include <stdio.h>
int main() {
int n, rev = 0;
scanf("%d", &n);
while (n != 0) {
rev = rev * 10 + n % 10; // pull off last digit
n /= 10;
}
printf("Reversed: %d\n", rev);
return 0;
}Output:
Reversed: 4321
The trick rev * 10 + n % 10 peels the last digit off n and appends it to rev each loop.
Example: a star pattern
#include <stdio.h>
int main() {
int rows = 4;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}*
* *
* * *
* * * *
The outer loop picks the row; the inner loop prints as many stars as the row number. Almost every pattern is a variation of this nested-loop idea.
How to practise well
- Write first, look later — attempt each program before reading any solution.
- Trace by hand — run a small input on paper to see the logic.
- Fix your own bugs — the errors you solve yourself stick best.
- Vary it — change a program slightly (descending pattern, different range) to test understanding.
Pick five programs from the lists above you have not written yet and code them today from a blank editor. Then modify each one slightly so it does something a little different.
Summary
- Practice is the only way to get fluent in C — reading is not enough.
- The 50 programs span numbers, patterns, arrays, strings and small projects.
- Classic programs (prime, reverse, palindrome, patterns) also appear in interviews.
- Always try writing a program yourself before checking a solution.
- Trace by hand and modify programs to deepen understanding.