Learn C with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
Hello World in C
#include <stdio.h>
int main() {
printf("Hello, World!\n");
int numbers[] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i];
}
printf("Sum: %d\n", sum);
return 0;
}
A basic C program demonstrating array manipulation, loops, and printf. Calculates sum of numbers in an array.
Sum of First N Numbers
#include <stdio.h>
int main() {
int n = 10, sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("Sum: %d\n", sum);
return 0;
}
Calculates the sum of first N natural numbers using a for loop and demonstrates basic arithmetic operations.
Factorial of a Number
#include <stdio.h>
int main() {
int n = 5, fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
printf("Factorial: %d\n", fact);
return 0;
}
Calculates factorial of a number using iterative multiplication. Demonstrates loops and basic math operations.
Check Even or Odd
#include <stdio.h>
int main() {
int n = 7;
if (n % 2 == 0)
printf("%d is Even\n", n);
else
printf("%d is Odd\n", n);
return 0;
}
Uses modulo operator to determine if a number is even or odd. Basic conditional logic demonstration.
Reverse a Number
#include <stdio.h>
int main() {
int n = 1234, rev = 0;
while (n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
printf("Reversed: %d\n", rev);
return 0;
}
Reverses a number using modulo and division operations. Shows digit manipulation and while loop usage.
Find Largest of Three Numbers
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 15;
if (a >= b && a >= c)
printf("Largest: %d\n", a);
else if (b >= a && b >= c)
printf("Largest: %d\n", b);
else
printf("Largest: %d\n", c);
return 0;
}
Finds the largest number among three using nested if-else statements. Demonstrates comparison operations.
Swap Two Numbers
#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
printf("a = %d, b = %d\n", a, b);
return 0;
}
Demonstrates swapping two numbers using a temporary variable. Basic variable manipulation example.
Fibonacci Series
#include <stdio.h>
int main() {
int n = 10, a = 0, b = 1, c;
printf("%d %d ", a, b);
for (int i = 2; i < n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
return 0;
}
Generates Fibonacci sequence using iteration. Shows sequence generation and variable updates.
Palindrome Check
#include <stdio.h>
int main() {
int n = 121, orig = n, rev = 0;
while (n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
if (orig == rev)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
}
Checks if a number reads the same forwards and backwards. Uses digit extraction and reversal.
Prime Number Check
#include <stdio.h>
int main() {
int n = 29, isPrime = 1;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime)
printf("%d is Prime\n", n);
else
printf("%d is Not Prime\n", n);
return 0;
}
Determines if a number is prime by checking divisibility up to square root. Shows optimization in prime checking.
Print Personal Information
#include <stdio.h>
int main() {
printf("Name: Saurav\n");
printf("Address: Nepal\n");
printf("Age: 21\n");
return 0;
}
Prints personal details like name, address, and age using printf statements.
Structure of C Program
#include <stdio.h>
int main() {
printf("This is a basic C program\n");
return 0;
}
Demonstrates the basic structure of a C program.
Syntax Error Example
#include <stdio.h>
int main() {
int a = 10;
printf("Value of a: %d\n", a);
return 0;
}
Contains a syntax error for debugging practice.
Logical Error Example
#include <stdio.h>
int main() {
int l = 10, b = 5;
int area = l + b;
printf("Area = %d\n", area);
return 0;
}
Program runs but gives incorrect output due to logical error.
Runtime Error Example
#include <stdio.h>
int main() {
int a = 10, b = 0;
int c = a / b;
printf("Result = %d\n", c);
return 0;
}
Demonstrates runtime error caused by division by zero.
Comments and Documentation Example
#include <stdio.h>
/*
Program Name: Simple Addition
Description: Adds two integers
Author: Student
*/
int main() {
int a = 5, b = 7;
int sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
Demonstrates use of comments and documentation in C.