Collection of 50+ important C programs frequently asked in exams and interviews covering patterns, arrays, strings, numbers, recursion, and sorting algorithms.
This collection contains the most important C programs asked in exams, lab assignments, and interviews. Each program includes a working solution with output. Practice these to build strong programming fundamentals.
Number Programs
1. Check Prime Number
#include <stdio.h>
int isPrime(int n) {
if (n <= 1) return 0;
if (n <= 3) return 1;
if (n % 2 == 0 || n % 3 == 0) return 0;
for (int i = 5; i * i <= n; i += 6)
if (n % i == 0 || n % (i + 2) == 0) return 0;
return 1;
}
int main() {
int num = 29;
printf("%d is %s\n", num, isPrime(num) ? "Prime" : "Not Prime");
return 0;
}
2. Fibonacci Series
#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1, next;
for (int i = 0; i < n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
}
printf("\n");
}
int main() {
fibonacci(10);
return 0;
}
3. Factorial (Iterative and Recursive)
#include <stdio.h>
long factorialIterative(int n) {
long result = 1;
for (int i = 2; i <= n; i++) result *= i;
return result;
}
long factorialRecursive(int n) {
if (n <= 1) return 1;
return n * factorialRecursive(n - 1);
}
int main() {
printf("5! = %ld (iterative)\n", factorialIterative(5));
printf("5! = %ld (recursive)\n", factorialRecursive(5));
return 0;
}
5! = 120 (iterative)
5! = 120 (recursive)
4. Reverse a Number
#include <stdio.h>
int reverseNumber(int n) {
int reversed = 0;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return reversed;
}
int main() {
printf("Reverse of 12345: %d\n", reverseNumber(12345));
return 0;
}
5. Check Palindrome Number
#include <stdio.h>
int isPalindrome(int n) {
int original = n, reversed = 0;
while (n > 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return original == reversed;
}
int main() {
printf("121: %s\n", isPalindrome(121) ? "Palindrome" : "Not Palindrome");
printf("123: %s\n", isPalindrome(123) ? "Palindrome" : "Not Palindrome");
return 0;
}
121: Palindrome
123: Not Palindrome
6. Armstrong Number
#include <stdio.h>
#include <math.h>
int isArmstrong(int n) {
int original = n, sum = 0, digits = 0, temp = n;
while (temp > 0) { digits++; temp /= 10; }
temp = n;
while (temp > 0) {
sum += (int)pow(temp % 10, digits);
temp /= 10;
}
return sum == original;
}
int main() {
printf("153: %s\n", isArmstrong(153) ? "Armstrong" : "Not Armstrong");
printf("370: %s\n", isArmstrong(370) ? "Armstrong" : "Not Armstrong");
return 0;
}
153: Armstrong
370: Armstrong
7. GCD and LCM
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) { int t = b; b = a % b; a = t; }
return a;
}
int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
int main() {
printf("GCD(12, 18) = %d\n", gcd(12, 18));
printf("LCM(12, 18) = %d\n", lcm(12, 18));
return 0;
}
GCD(12, 18) = 6
LCM(12, 18) = 36
8. Power of a Number
#include <stdio.h>
long power(int base, int exp) {
long result = 1;
while (exp > 0) {
if (exp % 2 == 1) result *= base;
base *= base;
exp /= 2;
}
return result;
}
int main() {
printf("2^10 = %ld\n", power(2, 10));
printf("3^5 = %ld\n", power(3, 5));
return 0;
}
Array Programs
9. Find Largest and Smallest Element
#include <stdio.h>
int main() {
int arr[] = {23, 55, 12, 89, 34, 7, 62};
int n = sizeof(arr) / sizeof(arr[0]);
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
printf("Largest: %d, Smallest: %d\n", max, min);
return 0;
}
10. Reverse an Array
#include <stdio.h>
void reverse(int arr[], int n) {
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
reverse(arr, 5);
for (int i = 0; i < 5; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
11. Bubble Sort
#include <stdio.h>
void bubbleSort(int arr[], int n) {
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;
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = 7;
bubbleSort(arr, n);
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
12. Binary Search
#include <stdio.h>
int binarySearch(int arr[], int n, int target) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 45, 56, 72};
int result = binarySearch(arr, 10, 23);
printf("Found at index: %d\n", result);
return 0;
}
String Programs
13. Reverse a String
#include <stdio.h>
#include <string.h>
void reverseString(char *str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = temp;
}
}
int main() {
char str[] = "Hello World";
reverseString(str);
printf("Reversed: %s\n", str);
return 0;
}
14. Check String Palindrome
#include <stdio.h>
#include <string.h>
int isStringPalindrome(const char *str) {
int left = 0, right = strlen(str) - 1;
while (left < right) {
if (str[left] != str[right]) return 0;
left++; right--;
}
return 1;
}
int main() {
printf("madam: %s\n", isStringPalindrome("madam") ? "Palindrome" : "Not");
printf("hello: %s\n", isStringPalindrome("hello") ? "Palindrome" : "Not");
return 0;
}
madam: Palindrome
hello: Not
15. Count Vowels and Consonants
#include <stdio.h>
#include <ctype.h>
void countChars(const char *str) {
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
for (int i = 0; str[i]; i++) {
char c = tolower(str[i]);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') vowels++;
else if (isalpha(c)) consonants++;
else if (isdigit(c)) digits++;
else if (c == ' ') spaces++;
}
printf("Vowels: %d, Consonants: %d, Digits: %d, Spaces: %d\n",
vowels, consonants, digits, spaces);
}
int main() {
countChars("Hello World 123");
return 0;
}
Vowels: 3, Consonants: 7, Digits: 3, Spaces: 2
Pattern Programs
16. Star Pyramid
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = rows - i; j > 0; j--) printf(" ");
for (int j = 1; j <= 2*i-1; j++) printf("*");
printf("\n");
}
return 0;
}
*
***
*****
*******
*********
17. Number Triangle
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++)
printf("%d ", j);
printf("\n");
}
return 0;
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Recursion Programs
18. Tower of Hanoi
#include <stdio.h>
void hanoi(int n, char from, char to, char aux) {
if (n == 0) return;
hanoi(n - 1, from, aux, to);
printf("Move disk %d: %c -> %c\n", n, from, to);
hanoi(n - 1, aux, to, from);
}
int main() {
hanoi(3, 'A', 'C', 'B');
return 0;
}
Move disk 1: A -> C
Move disk 2: A -> B
Move disk 1: C -> B
Move disk 3: A -> C
Move disk 1: B -> A
Move disk 2: B -> C
Move disk 1: A -> C
19. Sum of Digits (Recursive)
#include <stdio.h>
int sumDigits(int n) {
if (n == 0) return 0;
return n % 10 + sumDigits(n / 10);
}
int main() {
printf("Sum of digits of 12345: %d\n", sumDigits(12345));
return 0;
}
Sum of digits of 12345: 15
Sorting Algorithms
20. Selection Sort
#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[minIdx]) minIdx = j;
int temp = arr[i]; arr[i] = arr[minIdx]; arr[minIdx] = temp;
}
}
int main() {
int arr[] = {29, 10, 14, 37, 13};
selectionSort(arr, 5);
for (int i = 0; i < 5; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
21. Insertion Sort
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
insertionSort(arr, 5);
for (int i = 0; i < 5; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
Quick Reference: All Programs
| # | Program | Key Concept |
|---|
| 1-8 | Number programs | Loops, modulus, recursion |
| 9-12 | Array programs | Indexing, searching, sorting |
| 13-15 | String programs | Character arrays, traversal |
| 16-17 | Pattern programs | Nested loops |
| 18-19 | Recursion | Base case, divide & conquer |
| 20-21 | Sorting | Comparison-based algorithms |
Summary
These programs form the foundation of C programming proficiency. Each one teaches a specific concept – from basic loops and conditions to recursion and algorithms. Practice writing them from memory, then try variations (e.g., reverse pyramid, merge sort, string compression). The ability to code these quickly and correctly is what interviewers and examiners look for.