20 C programming examples for beginners
Each example below is short, self-contained, and ready to paste straight into the C compiler to run. Work through them in order — they build on each other, starting with basic output and ending with simple algorithms.
1. Hello, World!
Every C program needs a main() function — this is where execution starts.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
2. Adding two numbers
Reads two integers using scanf() and prints their sum.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", a + b);
return 0;
}
3. Checking even or odd
Uses the modulus operator % to test divisibility by 2.
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
4. Finding the largest of three numbers
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;
printf("Largest = %d\n", largest);
return 0;
}
5. Factorial of a number
A classic loop-based example — multiplies numbers from 1 up to n.
#include <stdio.h>
int main() {
int n;
long long fact = 1;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
fact *= i;
printf("Factorial = %lld\n", fact);
return 0;
}
6. Fibonacci series
#include <stdio.h>
int main() {
int n, a = 0, b = 1;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("%d ", a);
int next = a + b;
a = b;
b = next;
}
printf("\n");
return 0;
}
7. Checking a prime number
#include <stdio.h>
int main() {
int n, isPrime = 1;
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;
}
8. Reversing a number
#include <stdio.h>
int main() {
int n, reversed = 0;
scanf("%d", &n);
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
printf("Reversed = %d\n", reversed);
return 0;
}
9. Sum of digits
#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
while (n != 0) {
sum += n % 10;
n /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
10. Palindrome check (number)
#include <stdio.h>
int main() {
int n, original, reversed = 0;
scanf("%d", &n);
original = n;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
printf(original == reversed ? "Palindrome\n" : "Not a Palindrome\n");
return 0;
}
11. Printing a multiplication table
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= 10; i++)
printf("%d x %d = %d\n", n, i, n * i);
return 0;
}
12. Right-angled triangle pattern
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
13. Swapping two numbers (without a third variable)
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("a = %d, b = %d\n", a, b);
return 0;
}
14. Working with arrays: finding the maximum
#include <stdio.h>
int main() {
int arr[] = {12, 45, 3, 67, 21};
int n = 5, max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max) max = arr[i];
printf("Maximum = %d\n", max);
return 0;
}
15. Bubble sort
#include <stdio.h>
int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
int n = 6;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
16. String length without strlen()
#include <stdio.h>
int main() {
char str[] = "Hello";
int length = 0;
while (str[length] != '\0')
length++;
printf("Length = %d\n", length);
return 0;
}
17. Checking if a string is a palindrome
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "madam";
int len = strlen(str);
int isPalindrome = 1;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) { isPalindrome = 0; break; }
}
printf(isPalindrome ? "Palindrome\n" : "Not a Palindrome\n");
return 0;
}
18. Using functions: a simple calculator
#include <stdio.h>
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int main() {
int x = 10, y = 4;
printf("Sum = %d\n", add(x, y));
printf("Difference = %d\n", subtract(x, y));
return 0;
}
19. Using pointers
#include <stdio.h>
int main() {
int num = 25;
int *ptr = #
printf("Value = %d\n", *ptr);
printf("Address = %p\n", (void*)ptr);
return 0;
}
20. Structures: storing student data
#include <stdio.h>
struct Student {
char name[30];
int marks;
};
int main() {
struct Student s1 = {"Aman", 88};
printf("Name: %s, Marks: %d\n", s1.name, s1.marks);
return 0;
}
What to try next
Once these feel comfortable, look into pointers with arrays, dynamic memory using malloc(), and file handling with fopen(). If you hit a wall with an error message while experimenting, check our guide on common C compiler errors and how to fix them.