CodeMitra

Master 60+ C Programming Concepts

{ Advanced C Programming }

100+ Complete Programs with Examples & Explanations

๐Ÿ”ข Basic Programs & Fundamentals
Hello WorldC
#include <stdio.h>
 
int main() {
  printf("Hello, World!");
  return 0;
}
Output
Hello, World!

Classic first program. Entry point with main().

Simple Input/OutputC
#include <stdio.h>
 
int main() {
  int num;
  printf("Enter a number: ");
  scanf("%d", &num);
  printf("You entered: %d", 
         num);
}
Output (Input: 42)
Enter a number: 42 You entered: 42

Basic I/O with scanf and printf.

Add Two NumbersC
#include <stdio.h>
 
int main() {
  int a, b, sum;
  scanf("%d %d", &a, &b);
  sum = a + b;
  printf("%d", sum);
}
Output (5 + 3)
8

Basic arithmetic operation.

Subtract Two NumbersC
#include <stdio.h>
 
int main() {
  int a = 10, b = 3;
  printf("%d", a - b);
}
Output
7

Subtraction operation.

Multiply Two NumbersC
#include <stdio.h>
 
int main() {
  int a = 7, b = 8;
  printf("%d", a * b);
}
Output
56

Multiplication operation.

Divide Two NumbersC
#include <stdio.h>
 
int main() {
  int a = 20, b = 5;
  printf("%d", a / b);
}
Output
4

Integer division.

Modulo OperationC
#include <stdio.h>
 
int main() {
  int a = 17, b = 5;
  printf("%d", a % b);
}
Output
2

Remainder operation.

Area of CircleC
#include <stdio.h>
 
int main() {
  float radius = 5.0;
  float area = 3.14 * radius * 
                radius;
  printf("%.2f", area);
}
Output
78.50

ฯ€ ร— rยฒ. Formula application.

Simple InterestC
#include <stdio.h>
 
int main() {
  float p = 1000, r = 5, t = 2;
  float si = (p * r * t) / 100;
  printf("%.2f", si);
}
Output
100.00

SI = (Pร—Rร—T)/100

Increment & DecrementC
#include <stdio.h>
 
int main() {
  int x = 5;
  printf("%d %d", x++, ++x);
  return 0;
}
Output
5 7

Post/Pre increment differences.

๐Ÿ“ Mathematical Programs
Palindrome NumberC
int isPalindrome(int n) {
  int rev = 0, temp = n;
  while(n != 0) {
    rev = rev * 10 + n % 10;
    n /= 10;
  }
  return (temp == rev);
}
Output (121)
1 (True - Palindrome)

O(log n) complexity.

Prime Number CheckC
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)
      return 0;
  return 1;
}
Output (17)
1 (Prime)

O(โˆšn) with 6kยฑ1 optimization.

Factorial (Iterative)C
long long factorial(
    int n) {
  long long result = 1;
  for(int i = 2; i <= n; i++)
    result *= i;
  return result;
}
Output (5)
120

O(n), avoids stack issues.

Factorial (Recursive)C
long long fact(
    int n) {
  if(n == 0 || n == 1)
    return 1;
  return n * fact(n - 1);
}
Output (5)
120

Classic recursion example.

Fibonacci RecursiveC
int fib(int n) {
  if(n <= 1)
    return n;
  return fib(n-1) + 
         fib(n-2);
}
Output (10)
55

O(2^n), slow for large n.

Fibonacci IterativeC
void fibonacci(int n) {
  int 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;
  }
}
Output (10)
0 1 1 2 3 5 8 13 21 34

O(n) time, O(1) space.

Fibonacci MemoizationC
long long memo[100];
 
long long fib(int n) {
  if(n <= 1) return n;
  if(memo[n] != 0)
    return memo[n];
  memo[n] = fib(n-1) + 
            fib(n-2);
  return memo[n];
}
Output (50)
Instant result (cached)

O(n) with memoization.

GCD (Euclidean)C
int gcd(int a, int b) {
  if(b == 0)
    return a;
  return gcd(b, a % b);
}
Output (48, 18)
6

O(log min(a,b)).

LCM (Least Common Multiple)C
int lcm(int a, int b) {
  int gcd_val = gcd(a, b);
  return (a * b) / gcd_val;
}
 
int gcd(int a, int b) {
  while(b) {
    int t = b;
    b = a % b;
    a = t;
  }
  return a;
}
Output (12, 8)
24

Uses GCD formula.

Power FunctionC
long long power(
    long long x, 
    int y) {
  if(y == 0)
    return 1;
  long long res = 
    power(x, y/2);
  if(y % 2 == 0)
    return res * res;
  return x * res * res;
}
Output (2^10)
1024

Binary exponentiation, O(log y).

Armstrong NumberC
int isArmstrong(int n) {
  int orig = n, sum = 0;
  while(n > 0) {
    int d = n % 10;
    sum += d * d * d;
    n /= 10;
  }
  return (sum == orig);
}
Output (153)
1 (1ยณ+5ยณ+3ยณ=153)

Sum of digit cubes.

Perfect NumberC
int isPerfect(int n) {
  int sum = 0;
  for(int i = 1; i < n; i++)
    if(n % i == 0)
      sum += i;
  return (sum == n);
}
Output (6)
1 (1+2+3=6)

Sum of proper divisors.

Harshad NumberC
int isHarshad(int n) {
  int sum = 0, temp = n;
  while(temp > 0) {
    sum += temp % 10;
    temp /= 10;
  }
  return (n % sum == 0);
}
Output (18)
1 (18 % (1+8) = 0)

Divisible by digit sum.

Even or OddC
#include <stdio.h>
 
void checkEvenOdd(int n) {
  if(n % 2 == 0)
    printf("Even");
  else
    printf("Odd");
}
Output (5)
Odd

Modulo 2 check.

Count DigitsC
int countDigits(int n) {
  int count = 0;
  while(n != 0) {
    count++;
    n = n / 10;
  }
  return count;
}
Output (12345)
5

O(log n).

Sum of DigitsC
int sumOfDigits(int n) {
  int sum = 0;
  while(n > 0) {
    sum += n % 10;
    n = n / 10;
  }
  return sum;
}
Output (12345)
15

Sum all digits recursively.

Leap Year CheckC
int isLeapYear(int yr) {
  if((yr % 400 == 0) ||
     (yr % 4 == 0 && 
      yr % 100 != 0))
    return 1;
  return 0;
}
Output (2024)
1 (Leap Year)

Divisibility rules.

๐Ÿ“ String Programs
String LengthC
int stringLength(
    char str[]) {
  int count = 0;
  while(str[count] != '\0')
    count++;
  return count;
}
Output ("Hello")
5

Count until null terminator.

Reverse StringC
void reverseString(
    char str[]) {
  int n = strlen(str);
  for(int i = 0; i < n/2; 
      i++) {
    char t = str[i];
    str[i] = str[n-i-1];
    str[n-i-1] = t;
  }
}
Output ("hello")
olleh

O(n) time, O(1) space.

String PalindromeC
int isPalindrome(
    char str[]) {
  int n = strlen(str);
  for(int i = 0; 
      i < n/2; i++)
    if(str[i] != str[n-i-1])
      return 0;
  return 1;
}
Output ("racecar")
1 (True)

Check string equality backward.

String ConcatenationC
void concat(
    char dest[], 
    char src[]) {
  int i = strlen(dest);
  int j = 0;
  while(src[j] != '\0') {
    dest[i++] = src[j++];
  }
  dest[i] = '\0';
}
Output ("Hello" + " World")
Hello World

Manual string join.

String CompareC
int strCompare(
    char s1[], 
    char s2[]) {
  while(*s1 && *s1 == *s2) {
    s1++;
    s2++;
  }
  return *s1 - *s2;
}
Output ("abc", "abc")
0 (Equal)

Compare two strings.

Check SubstringC
int hasSubstring(
    char str[], 
    char sub[]) {
  int n = strlen(str);
  int m = strlen(sub);
  for(int i = 0; i <= n-m; i++) {
    int j;
    for(j = 0; j < m; j++)
      if(str[i+j] != sub[j])
        break;
    if(j == m) return i;
  }
  return -1;
}
Output ("hello", "ll")
2

Find substring index.

Uppercase ConversionC
void toUppercase(
    char str[]) {
  for(int i = 0; 
      str[i] != '\0'; i++) {
    if(str[i] >= 'a' && 
        str[i] <= 'z')
      str[i] = str[i] - 32;
  }
}
Output ("hello")
HELLO

ASCII manipulation.

Lowercase ConversionC
void toLowercase(
    char str[]) {
  for(int i = 0; 
      str[i] != '\0'; i++) {
    if(str[i] >= 'A' && 
        str[i] <= 'Z')
      str[i] = str[i] + 32;
  }
}
Output ("HELLO")
hello

Convert uppercase to lowercase.

Character FrequencyC
void charFrequency(
    char str[]) {
  int freq[256] = {0};
  for(int i = 0; 
      str[i] != '\0'; i++)
    freq[str[i]]++;
  for(int i = 0; i < 256; i++)
    if(freq[i] > 0)
      printf("%c: %d\n", 
             i, freq[i]);
}
Output ("aabbcc")
a: 2, b: 2, c: 2

Count char occurrences.

Vowel or ConsonantC
void checkVowel(
    char ch) {
  ch = tolower(ch);
  if(ch == 'a' || 
     ch == 'e' ||
     ch == 'i' || 
     ch == 'o' ||
     ch == 'u')
    printf("Vowel");
}
Output ('a')
Vowel

5 vowels check.

Word CountC
int wordCount(
    char str[]) {
  int count = 0;
  for(int i = 0; 
      str[i] != '\0'; i++) {
    if(str[i] == ' ')
      count++;
  }
  return count + 1;
}
Output ("hello world test")
3

Count words via spaces.

๐Ÿ“Š Array Programs
Sum of Array ElementsC
int sumArray(
    int arr[], 
    int n) {
  int sum = 0;
  for(int i = 0; i < n; i++)
    sum += arr[i];
  return sum;
}
Output (1,2,3,4,5)
15

O(n).

Average of ArrayC
double average(
    int arr[], 
    int n) {
  int sum = 0;
  for(int i = 0; i < n; i++)
    sum += arr[i];
  return 
    ((double)sum / n);
}
Output (10,20,30)
20.00

Average calculation.

Find MaximumC
int findMax(
    int arr[], 
    int n) {
  int max = arr[0];
  for(int i = 1; i < n; i++)
    if(arr[i] > max)
      max = arr[i];
  return max;
}
Output (5,2,8,1,9)
9

O(n).

Find MinimumC
int findMin(
    int arr[], 
    int n) {
  int min = arr[0];
  for(int i = 1; i < n; i++)
    if(arr[i] < min)
      min = arr[i];
  return min;
}
Output (5,2,8,1,9)
1

O(n).

Reverse ArrayC
void reverseArray(
    int arr[], 
    int n) {
  int start = 0, end = n-1;
  while(start < end) {
    int t = arr[start];
    arr[start] = arr[end];
    arr[end] = t;
    start++;
    end--;
  }
}
Output (1,2,3,4,5)
5,4,3,2,1

O(n) time, O(1) space.

Check Duplicate ElementsC
void checkDuplicate(
    int arr[], 
    int n) {
  for(int i = 0; i < n; i++) {
    for(int j = i+1; j < n; j++) {
      if(arr[i] == arr[j]) {
        printf("%d dup\n",
               arr[i]);
      }
    }
  }
}
Output (1,2,3,2,4)
2 is duplicate

O(nยฒ) brute force.

Copy ArrayC
void copyArray(
    int src[], 
    int dest[], 
    int n) {
  for(int i = 0; i < n; i++)
    dest[i] = src[i];
}
Output
All elements copied

Element-wise copy.

Swap Two ElementsC
void swap(
    int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}
Output (a=5, b=10)
a=10, b=5

Using pointers.

Merge Two ArraysC
void mergeArrays(
    int a[], int n1,
    int b[], int n2,
    int result[]) {
  for(int i = 0; i < n1; i++)
    result[i] = a[i];
  for(int i = 0; i < n2; i++)
    result[n1+i] = b[i];
}
Output
Combined array

O(n1 + n2).

Linear SearchC
int linearSearch(
    int arr[], 
    int n, 
    int key) {
  for(int i = 0; i < n; i++) {
    if(arr[i] == key)
      return i;
  }
  return -1;
}
Output (key=9)
Index: 3

O(n). Unsorted arrays.

2D Array SumC
int sum2DArray(
    int arr[][3], 
    int rows,
    int cols) {
  int sum = 0;
  for(int i = 0; i < rows; i++)
    for(int j = 0; j < cols; j++)
      sum += arr[i][j];
  return sum;
}
Output
Sum of all elements

O(rows ร— cols).

๐Ÿ”„ Sorting Algorithms
Bubble SortC
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 t = arr[j];
        arr[j] = arr[j+1];
        arr[j+1] = t;
      }
}
Output
11 12 22 25 64

O(nยฒ). Educational.

Selection SortC
void selectionSort(
    int arr[], 
    int n) {
  for(int i = 0; i < n-1; i++) {
    int min = i;
    for(int j = i+1; j < n; j++)
      if(arr[j] < arr[min])
        min = j;
    int t = arr[i];
    arr[i] = arr[min];
    arr[min] = t;
  }
}
Output
12 25 34 64

O(nยฒ) always.

Insertion SortC
void insertionSort(
    int arr[], 
    int n) {
  for(int i = 1; i < n; i++) {
    int key = arr[i];
    int j = i - 1;
    while(j >= 0 && 
          arr[j] > key) {
      arr[j+1] = arr[j];
      j--;
    }
    arr[j+1] = key;
  }
}
Output
12 25 34 64

O(nยฒ) worst, O(n) best.

Quick SortC
int partition(int arr[], 
    int low, int high) {
  int pivot = arr[high];
  int i = low - 1;
  for(int j = low; j < high; j++)
    if(arr[j] < pivot) {
      i++;
      int t = arr[i];
      arr[i] = arr[j];
      arr[j] = t;
    }
  int t = arr[i+1];
  arr[i+1] = arr[high];
  arr[high] = t;
  return i + 1;
}
Output
12 25 34 64

O(n log n) avg.

Merge SortC
void merge(int arr[], 
    int l, int m, int r) {
  int n1 = m - l + 1;
  int n2 = r - m;
  int L[n1], R[n2];
  
  for(int i = 0; i < n1; i++)
    L[i] = arr[l + i];
  for(int j = 0; j < n2; j++)
    R[j] = arr[m + 1 + j];
  
  int i = 0, j = 0, k = l;
  while(i < n1 && j < n2)
    arr[k++] = (L[i] <= R[j]) ? 
               L[i++] : R[j++];
}
Output
12 25 34 64

O(n log n) guaranteed.

Heap SortC
void heapify(
    int arr[], int n, 
    int i) {
  int largest = i;
  int left = 2*i + 1;
  int right = 2*i + 2;
  
  if(left < n && 
     arr[left] > arr[largest])
    largest = left;
  if(right < n && 
     arr[right] > arr[largest])
    largest = right;
  if(largest != i) {
    int t = arr[i];
    arr[i] = arr[largest];
    arr[largest] = t;
    heapify(arr, n, largest);
  }
}
Output
12 25 34 64

O(n log n) always.

Counting SortC
void countingSort(
    int arr[], int n) {
  int max = arr[0];
  for(int i = 1; i < n; i++)
    if(arr[i] > max)
      max = arr[i];
  
  int count[max+1] = {0};
  for(int i = 0; i < n; i++)
    count[arr[i]]++;
  
  int idx = 0;
  for(int i = 0; i <= max; i++)
    while(count[i] > 0)
      arr[idx++] = i;
}
Output
12 25 34 64

O(n + k).

๐Ÿ” Searching Algorithms
Binary SearchC
int binarySearch(
    int arr[], 
    int n, 
    int target) {
  int left = 0, right = n-1;
  while(left <= right) {
    int mid = left + 
              (right - left) / 2;
    if(arr[mid] == target)
      return mid;
    else if(arr[mid] < target)
      left = mid + 1;
    else
      right = mid - 1;
  }
  return -1;
}
Output (target: 25)
Index: 4

O(log n). Sorted only.

Linear SearchC
int linearSearch(
    int arr[], 
    int n, 
    int key) {
  for(int i = 0; i < n; i++) {
    if(arr[i] == key)
      return i;
  }
  return -1;
}
Output (key: 9)
Index: 3

O(n). Any array.

Jump SearchC
#include <math.h>
 
int jumpSearch(
    int arr[], 
    int n, 
    int x) {
  int step = sqrt(n);
  int prev = 0;
  while(arr[step-1] < x) {
    prev = step;
    step += sqrt(n);
    if(prev >= n) 
      return -1;
  }
  while(arr[prev] < x) {
    prev++;
    if(prev == step || 
        prev == n)
      return -1;
  }
  if(arr[prev] == x)
    return prev;
  return -1;
}
Output
Index found

O(โˆšn).

Interpolation SearchC
int interpolationSearch(
    int arr[], 
    int n, 
    int x) {
  int low = 0, high = n-1;
  while(low <= high && 
        x >= arr[low] && 
        x <= arr[high]) {
    int pos = low + 
      (((x - arr[low]) / 
        (arr[high] - arr[low])) 
      * (high - low));
    
    if(arr[pos] == x)
      return pos;
    if(arr[pos] < x)
      low = pos + 1;
    else
      high = pos - 1;
  }
  return -1;
}
Output
Index found

O(log log n) best case.

๐Ÿ”‚ Recursion Programs
Tower of HanoiC
void hanoi(int n, 
    char source, 
    char dest, 
    char aux) {
  if(n == 1) {
    printf("%c->%c\n", 
           source, dest);
    return;
  }
  hanoi(n-1, source, aux, dest);
  printf("%c->%c\n", 
         source, dest);
  hanoi(n-1, aux, dest, source);
}
Output (n=2, A-B-C)
Aโ†’C
Aโ†’B
Cโ†’B

O(2^n) moves.

GCD RecursionC
int gcd(int a, int b) {
  if(b == 0)
    return a;
  return gcd(b, a % b);
}
Output (48, 18)
6

Euclidean algorithm.

Sum of N NumbersC
int sumN(int n) {
  if(n == 0)
    return 0;
  return n + sumN(n - 1);
}
Output (5)
15

Sum 1+2+...+n.

Product of N NumbersC
int productN(int n) {
  if(n == 0 || n == 1)
    return 1;
  return n * productN(n - 1);
}
Output (5)
120

Product = factorial.

String Reversal RecursionC
void reverseStr(
    char str[], 
    int n) {
  if(n == 0)
    return;
  printf("%c", str[n-1]);
  reverseStr(str, n-1);
}
Output ("hello")
olleh

Print reversed.

Check Palindrome RecursionC
int isPalindrome(
    char str[], 
    int start, 
    int end) {
  if(start >= end)
    return 1;
  if(str[start] != str[end])
    return 0;
  return isPalindrome(str, 
         start+1, end-1);
}
Output ("racecar")
1 (True)

Two-pointer approach.

โš™๏ธ Bitwise Operations
Swap Using XORC
void swap(
    int *a, int *b) {
  *a = *a ^ *b;
  *b = *a ^ *b;
  *a = *a ^ *b;
}
Output (5, 10)
a=10, b=5

No temp variable.

Check Kth BitC
int checkKthBit(
    int n, int k) {
  return (n >> k) & 1;
}
Output (5, 1)
0 (0101, bit 1 = 0)

Right shift & AND.

Set Kth BitC
int setKthBit(
    int n, int k) {
  return n | (1 << k);
}
Output (5, 1)
7 (0101 โ†’ 0111)

OR with shifted 1.

Clear Kth BitC
int clearKthBit(
    int n, int k) {
  return n & ~(1 << k);
}
Output (7, 1)
5 (0111 โ†’ 0101)

AND with negated.

Toggle Kth BitC
int toggleKthBit(
    int n, int k) {
  return n ^ (1 << k);
}
Output (5, 1)
7 (0101 โ†’ 0111)

XOR flips bit.

Count Set BitsC
int countSetBits(
    int n) {
  int count = 0;
  while(n) {
    count += n & 1;
    n >>= 1;
  }
  return count;
}
Output (7)
3 (0111 has 3 ones)

Count 1 bits.

Is Power of 2C
int isPowerOf2(
    int n) {
  return (n > 0) && 
    ((n & (n-1)) == 0);
}
Output (8)
1 (2^3)

Single bit trick.

Multiply by Power of 2C
int multiplyBy2Power(
    int n, int p) {
  return n << p;
}
Output (5, 2)
20 (5 ร— 2^2)

Left shift = multiply.

Divide by Power of 2C
int divideBy2Power(
    int n, int p) {
  return n >> p;
}
Output (20, 2)
5 (20 รท 2^2)

Right shift = divide.

โ„น๏ธ About CodeMitra

CodeMitra is your comprehensive learning platform for mastering programming fundamentals. We provide 100+ complete C programs with detailed explanations, time complexity analysis, and real-world examples.

Our mission is to help students, developers, and programmers understand core concepts through practical code examples and interactive learning. Whether you're a beginner or advanced learner, CodeMitra has something for everyone.

โœจ Features

  • โœ… 100+ Complete C Programs
  • โœ… Output Examples for Each Program
  • โœ… Time & Space Complexity Analysis
  • โœ… Categorized Learning Path
  • โœ… Search Functionality
  • โœ… Professional Code Syntax
๐Ÿ Python Programming

Coming Soon! We're preparing a comprehensive Python programming guide with 100+ programs covering:

๐Ÿ“š Core Concepts

Variables, loops, functions, OOP

๐Ÿ”ง Data Structures

Lists, dicts, sets, tuples

๐ŸŽฏ Algorithms

Sorting, searching, DP

๐Ÿ’พ File Handling

Read, write, JSON, CSV

๐Ÿš€ Check back soon for the complete Python guide!

๐Ÿ“ง Get In Touch

Have questions, suggestions, or feedback? We'd love to hear from you! Connect with us through any of these channels:

๐Ÿ“ง Email

support@codemitra.com

Quick response within 24 hours

๐Ÿ’ฌ Social Media

@CodeMitra

Follow on all platforms

๐Ÿ’ก Feedback Form

feedback.codemitra.com

Share your suggestions

๐ŸŽฏ What We're Looking For:

  • Bug reports and issues
  • Feature requests and suggestions
  • Program contributions
  • Partnership opportunities

๐Ÿ“š 100+ Advanced Programming Concepts | C Language Algorithms & Data Structures

Master these fundamentals for better problem-solving and optimization skills

โœ“ 100+ Complete C Programs with Output Examples & Complexity Analysis