Common C Programs
Mastering C programming requires writing programs regularly. This section covers a curated set of commonly asked programs in academic exams, technical interviews, and competitive programming — with clean, well-explained code for each.
Number-Based Programs
1. Check Armstrong Number
An Armstrong number (also called a narcissistic number) is a number equal to the sum of its own digits each raised to the power of the number of digits. For example: 153 = 1³ + 5³ + 3³ = 153.
#include <stdio.h>
#include <math.h>
int main() {
int num, original, sum = 0, digits = 0;
int temp;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
temp = num;
// Count digits
while (temp != 0) { digits++; temp /= 10; }
temp = num;
// Calculate sum of digits^power
while (temp != 0) {
int d = temp % 10;
sum += (int)pow(d, digits);
temp /= 10;
}
if (sum == original)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is NOT an Armstrong number.\n", num);
return 0;
}
Sample Output
Enter a number: 153
153 is an Armstrong number.
Enter a number: 9474
9474 is an Armstrong number.
2. Check Palindrome Number
A number is a palindrome if it reads the same forward and backward. Example: 121, 1221, 12321.
#include <stdio.h>
int main() {
int num, reversed = 0, original;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
if (reversed == original)
printf("%d is a Palindrome.\n", original);
else
printf("%d is NOT a Palindrome.\n", original);
return 0;
}
Sample Output
Enter a number: 121
121 is a Palindrome.
3. Check Perfect Number
A perfect number equals the sum of its proper divisors (excluding itself). Example: 6 = 1 + 2 + 3 = 6.
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i < num; i++)
if (num % i == 0)
sum += i;
if (sum == num)
printf("%d is a Perfect number.\n", num);
else
printf("%d is NOT a Perfect number.\n", num);
return 0;
}
Sample Output
Enter a number: 28
28 is a Perfect number.
4. Print All Prime Numbers in a Range
#include <stdio.h>
int isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
int main() {
int low, high;
printf("Enter range (low high): ");
scanf("%d %d", &low, &high);
printf("Primes from %d to %d: ", low, high);
for (int i = low; i <= high; i++)
if (isPrime(i)) printf("%d ", i);
printf("\n");
return 0;
}
Sample Output
Enter range (low high): 1 30
Primes from 1 to 30: 2 3 5 7 11 13 17 19 23 29
5. GCD and LCM
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
int g = gcd(a, b);
int l = (a * b) / g;
printf("GCD = %d\n", g);
printf("LCM = %d\n", l);
return 0;
}
Sample Output
Enter two numbers: 12 18
GCD = 6
LCM = 36
Pattern Programs
6. Right Triangle Star Pattern
#include <stdio.h>
int main() {
int n;
printf("Enter rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
Output (n=5)
*
* *
* * *
* * * *
* * * * *
7. Number Pyramid
#include <stdio.h>
int main() {
int n;
printf("Enter rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
// Print leading spaces
for (int s = 1; s <= n - i; s++) printf(" ");
// Print numbers
for (int j = 1; j <= (2 * i - 1); j++) printf("%d ", j);
printf("\n");
}
return 0;
}
Output (n=4)
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
8. Floyd's Triangle
#include <stdio.h>
int main() {
int n, num = 1;
printf("Enter rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
printf("%-4d", num++);
printf("\n");
}
return 0;
}
Output (n=4)
1
2 3
4 5 6
7 8 9 10
Array Programs
9. Remove Duplicates from Array
#include <stdio.h>
int main() {
int arr[] = {1, 3, 2, 3, 5, 1, 4, 5};
int n = 8;
int unique[8], count = 0;
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < count; j++)
if (unique[j] == arr[i]) { found = 1; break; }
if (!found) unique[count++] = arr[i];
}
printf("Unique elements: ");
for (int i = 0; i < count; i++) printf("%d ", unique[i]);
printf("\n");
return 0;
}
Output
Unique elements: 1 3 2 5 4
10. Rotate Array Left by K Positions
#include <stdio.h>
void rotateLeft(int arr[], int n, int k) {
k = k % n;
int temp[k];
for (int i = 0; i < k; i++) temp[i] = arr[i];
for (int i = 0; i < n - k; i++) arr[i] = arr[i + k];
for (int i = 0; i < k; i++) arr[n - k + i] = temp[i];
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5, k = 2;
printf("Before: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
rotateLeft(arr, n, k);
printf("\nAfter rotating left by %d: ", k);
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
Output
Before: 1 2 3 4 5
After rotating left by 2: 3 4 5 1 2
String Programs
11. Check Palindrome String
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
int len = strlen(str);
int isPalin = 1;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
isPalin = 0;
break;
}
}
if (isPalin)
printf("\"%s\" is a Palindrome.\n", str);
else
printf("\"%s\" is NOT a Palindrome.\n", str);
return 0;
}
Sample Output
Enter a string: madam
"madam" is a Palindrome.
12. Count Words in a String
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char sentence[200];
printf("Enter a sentence: ");
fgets(sentence, 200, stdin);
int words = 0, inWord = 0;
for (int i = 0; sentence[i]; i++) {
if (!isspace((unsigned char)sentence[i])) {
if (!inWord) { words++; inWord = 1; }
} else {
inWord = 0;
}
}
printf("Word count: %d\n", words);
return 0;
}
Sample Output
Enter a sentence: C programming is fun and powerful
Word count: 6
Matrix Programs
13. Matrix Multiplication
#include <stdio.h>
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2] = {{0}};
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
C[i][j] += A[i][k] * B[k][j];
printf("Product:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++)
printf("%4d", C[i][j]);
printf("\n");
}
return 0;
}
Output
Product:
19 22
43 50
14. Transpose of a Matrix
#include <stdio.h>
int main() {
int mat[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int trans[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
trans[j][i] = mat[i][j];
printf("Transpose:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) printf("%3d", trans[i][j]);
printf("\n");
}
return 0;
}
Output
Transpose:
1 4 7
2 5 8
3 6 9
Recursion Programs
15. Binary to Decimal and Decimal to Binary
#include <stdio.h>
#include <math.h>
int binToDec(int bin) {
int dec = 0, base = 1;
while (bin > 0) {
dec += (bin % 10) * base;
bin /= 10;
base *= 2;
}
return dec;
}
void decToBin(int dec) {
if (dec == 0) return;
decToBin(dec / 2);
printf("%d", dec % 2);
}
int main() {
printf("Binary 1101 = %d (decimal)\n", binToDec(1101));
printf("Decimal 13 = ");
decToBin(13);
printf(" (binary)\n");
return 0;
}
Output
Binary 1101 = 13 (decimal)
Decimal 13 = 1101 (binary)
Summary
These programs cover the most frequently encountered C programming problems in exams and technical interviews. Practicing each one builds fluency in C syntax, problem-solving logic, and common algorithmic patterns. The key skills exercised here — number theory, array manipulation, string processing, pattern printing, matrix operations, and recursion — form the core competency expected of any C programmer at beginner to intermediate level. Writing these programs from scratch, modifying them, and combining concepts together is the best way to solidify C programming skills.
