ADVERTISEMENT

ADVERTISEMENT

Top 50 C Programs

hello_world.c

                        // 1. hello_world.c
#include <stdio.h>

int main() {
    // Print "Hello, World!" to the console
    printf("Hello, World!\n");
    return 0;
}                    

Last Modified: January 12, 2025 19:05:29

sum_of_two_numbers.c

                        // 2. sum_of_two_numbers.c
#include <stdio.h>

int main() {
    int a, b, sum;
    // Input two integers
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);
    
    // Calculate sum
    sum = a + b;
    printf("Sum: %d\n", sum);
    return 0;
}                    

Last Modified: January 12, 2025 19:05:54

difference_of_two_numbers.c

                        // 3. difference_of_two_numbers.c
#include <stdio.h>

int main() {
    int a, b, difference;
    // Input two integers
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);
    
    // Calculate difference
    difference = a - b;
    printf("Difference: %d\n", difference);
    return 0;
}                    

Last Modified: January 12, 2025 19:06:17

multiplication_of_two_numbers.c

                        // 4. multiplication_of_two_numbers.c
#include <stdio.h>

int main() {
    int a, b, product;
    // Input two integers
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);
    
    // Calculate product
    product = a * b;
    printf("Product: %d\n", product);
    return 0;
}                    

Last Modified: January 12, 2025 19:08:16

division_of_two_numbers.c

                        // 5. division_of_two_numbers.c
#include <stdio.h>

int main() {
    int a, b;
    float result;
    // Input two integers
    printf("Enter two integers (dividend and divisor): ");
    scanf("%d %d", &a, &b);
    
    // Check for division by zero
    if (b != 0) {
        result = (float)a / b;
        printf("Result: %.2f\n", result);
    } else {
        printf("Error: Division by zero is not allowed.\n");
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:08:37

swap_two_numbers_with_variable.c

                        // 6. swap_two_numbers_with_variable.c
#include <stdio.h>

int main() {
    int a, b, temp;
    // Input two integers
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);
    
    // Swap using a temporary variable
    temp = a;
    a = b;
    b = temp;
    
    printf("After swapping: a = %d, b = %d\n", a, b);
    return 0;
}                    

Last Modified: January 12, 2025 19:09:07

swap_two_numbers_without_variable.c

                        // 7. swap_two_numbers_without_variable.c
#include <stdio.h>

int main() {
    int a, b;
    // Input two integers
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);
    
    // Swap without a temporary variable
    a = a + b;
    b = a - b;
    a = a - b;
    
    printf("After swapping: a = %d, b = %d\n", a, b);
    return 0;
}                    

Last Modified: January 12, 2025 19:10:27

check_even_or_odd.c

                        // 8. check_even_or_odd.c
#include <stdio.h>

int main() {
    int num;
    // Input an integer
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    // Check if the number is even or odd
    if (num % 2 == 0) {
        printf("%d is even.\n", num);
    } else {
        printf("%d is odd.\n", num);
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:10:48

largest_of_two_numbers.c

                        // 9. largest_of_two_numbers.c
#include <stdio.h>

int main() {
    int a, b;
    // Input two integers
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);
    
    // Find the largest number
    if (a > b) {
        printf("%d is larger.\n", a);
    } else if (b > a) {
        printf("%d is larger.\n", b);
    } else {
        printf("Both numbers are equal.\n");
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:11:09

largest_of_three_numbers.c

                        // 10. largest_of_three_numbers.c
#include <stdio.h>

int main() {
    int a, b, c;
    // Input three integers
    printf("Enter three integers: ");
    scanf("%d %d %d", &a, &b, &c);
    
    // Find the largest number
    if (a >= b && a >= c) {
        printf("%d is the largest.\n", a);
    } else if (b >= a && b >= c) {
        printf("%d is the largest.\n", b);
    } else {
        printf("%d is the largest.\n", c);
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:11:30

check_prime_number.c

                        // 11. check_prime_number.c
#include <stdio.h>

int main() {
    int num, i, is_prime = 1;
    // Input an integer
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    if (num <= 1) {
        printf("%d is not a prime number.\n", num);
        return 0;
    }
    
    // Check divisors from 2 to sqrt(num)
    for (i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            is_prime = 0;
            break;
        }
    }
    
    if (is_prime) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is not a prime number.\n", num);
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:11:51

print_numbers_from_1_to_n.c

                        // 1. print_numbers_from_1_to_n.c
#include <stdio.h>

int main() {
    int n;
    // Input the value of N
    printf("Enter a number: ");
    scanf("%d", &n);

    printf("Numbers from 1 to %d:\n", n);
    for (int i = 1; i <= n; i++) {
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}                    

Last Modified: January 12, 2025 19:15:17

print_multiplication_table.c

                        // 2. print_multiplication_table.c
#include <stdio.h>

int main() {
    int num;
    // Input the number for multiplication table
    printf("Enter a number: ");
    scanf("%d", &num);

    printf("Multiplication table of %d:\n", num);
    for (int i = 1; i <= 10; i++) {
        printf("%d x %d = %d\n", num, i, num * i);
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:15:44

sum_of_first_n_natural_numbers.c

                        // 3. sum_of_first_n_natural_numbers.c
#include <stdio.h>

int main() {
    int n, sum = 0;
    // Input the value of N
    printf("Enter a number: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    printf("Sum of first %d natural numbers is %d\n", n, sum);
    return 0;
}                    

Last Modified: January 12, 2025 19:16:03

reverse_a_number.c

                        // 4. reverse_a_number.c
#include <stdio.h>

int main() {
    int num, reversed = 0;
    // Input a number
    printf("Enter a number: ");
    scanf("%d", &num);

    while (num != 0) {
        reversed = reversed * 10 + num % 10;
        num /= 10;
    }
    printf("Reversed number is %d\n", reversed);
    return 0;
}                    

Last Modified: January 12, 2025 19:16:24

count_digits_in_a_number.c

                        // 5. count_digits_in_a_number.c
#include <stdio.h>

int main() {
    int num, count = 0;
    // Input a number
    printf("Enter a number: ");
    scanf("%d", &num);

    do {
        count++;
        num /= 10;
    } while (num != 0);

    printf("Number of digits: %d\n", count);
    return 0;
}                    

Last Modified: January 12, 2025 19:17:11

palindrome_number.c

                        // 6. palindrome_number.c
#include <stdio.h>

int main() {
    int num, reversed = 0, original;
    // Input a number
    printf("Enter a number: ");
    scanf("%d", &num);

    original = num;
    while (num != 0) {
        reversed = reversed * 10 + num % 10;
        num /= 10;
    }

    if (original == reversed) {
        printf("%d is a palindrome.\n", original);
    } else {
        printf("%d is not a palindrome.\n", original);
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:17:35

armstrong_number.c

                        #include <stdio.h>
#include <math.h>

int main() {
    int num, original, remainder, n = 0;
    double result = 0.0;
    // Input a number
    printf("Enter a number: ");
    scanf("%d", &num);

    original = num;
    while (original != 0) {
        original /= 10;
        n++;
    }

    original = num;
    while (original != 0) {
        remainder = original % 10;
        result += pow(remainder, n);
        original /= 10;
    }

    if ((int)result == num) {
        printf("%d is an Armstrong number.\n", num);
    } else {
        printf("%d is not an Armstrong number.\n", num);
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:18:02

fibonacci_series.c

                        // 8. fibonacci_series.c
#include <stdio.h>

int main() {
    int n, t1 = 0, t2 = 1, nextTerm;
    // Input the number of terms
    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: %d, %d", t1, t2);
    for (int i = 3; i <= n; i++) {
        nextTerm = t1 + t2;
        printf(", %d", nextTerm);
        t1 = t2;
        t2 = nextTerm;
    }
    printf("\n");
    return 0;
}                    

Last Modified: January 12, 2025 19:18:24

factorial_of_a_number.c

                        // 9. factorial_of_a_number.c
#include <stdio.h>

// Function to calculate factorial using recursion
int factorial_recursive(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    return n * factorial_recursive(n - 1);
}

// Function to calculate factorial using iteration
int factorial_iterative(int n) {
    int fact = 1;
    for (int i = 1; i <= n; i++) {
        fact *= i;
    }
    return fact;
}

int main() {
    int n;
    // Input a number
    printf("Enter a number: ");
    scanf("%d", &n);

    printf("Factorial (Recursive): %d\n", factorial_recursive(n));
    printf("Factorial (Iterative): %d\n", factorial_iterative(n));

    return 0;
}                    

Last Modified: January 12, 2025 19:19:11

sum_of_digits_in_a_number.c

                        // 10. sum_of_digits_in_a_number.c
#include <stdio.h>

int main() {
    int num, sum = 0;
    // Input a number
    printf("Enter a number: ");
    scanf("%d", &num);

    while (num != 0) {
        sum += num % 10;
        num /= 10;
    }
    printf("Sum of digits: %d\n", sum);
    return 0;
}                    

Last Modified: January 12, 2025 19:19:29

find_largest_element_in_array.c

                        #include <stdio.h>

int main() {
    int n, i, largest;
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int arr[n];
    printf("Enter the elements: ");
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    largest = arr[0]; // Assume the first element is the largest
    for (i = 1; i < n; i++) {
        if (arr[i] > largest) {
            largest = arr[i]; // Update largest if current element is larger
        }
    }

    printf("The largest element in the array is: %d\n", largest);
    return 0;
}                    

Last Modified: January 12, 2025 19:22:46

reverse_array.c

                        #include <stdio.h>

int main() {
    int n, i;
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int arr[n], reversed[n];
    printf("Enter the elements: ");
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Reversing the array
    for (i = 0; i < n; i++) {
        reversed[i] = arr[n - 1 - i];
    }

    printf("Reversed array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", reversed[i]);
    }
    printf("\n");
    return 0;
}                    

Last Modified: January 12, 2025 19:23:04

bubble_sort.c

                        #include <stdio.h>

int main() {
    int n, i, j, temp;
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int arr[n];
    printf("Enter the elements: ");
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Bubble sort algorithm
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap arr[j] and arr[j+1]
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    printf("Sorted array (Bubble Sort): ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}                    

Last Modified: January 12, 2025 19:23:20

selection_sort.c

                        #include <stdio.h>

int main() {
    int n, i, j, minIdx, temp;
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int arr[n];
    printf("Enter the elements: ");
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Selection sort algorithm
    for (i = 0; i < n - 1; i++) {
        minIdx = i;
        for (j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIdx]) {
                minIdx = j;
            }
        }
        // Swap the found minimum element with the first element
        temp = arr[i];
        arr[i] = arr[minIdx];
        arr[minIdx] = temp;
    }

    printf("Sorted array (Selection Sort): ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}                    

Last Modified: January 12, 2025 19:23:40

linear_search.c

                        #include <stdio.h>

int main() {
    int n, key, i, found = 0;
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int arr[n];
    printf("Enter the elements: ");
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Enter the element to search: ");
    scanf("%d", &key);

    // Linear search algorithm
    for (i = 0; i < n; i++) {
        if (arr[i] == key) {
            found = 1;
            break;
        }
    }

    if (found) {
        printf("Element found at index %d\n", i);
    } else {
        printf("Element not found\n");
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:24:03

binary_search.c

                        #include <stdio.h>

int binary_search(int arr[], int n, int key) {
    int left = 0, right = n - 1, mid;
    while (left <= right) {
        mid = (left + right) / 2;
        if (arr[mid] == key) {
            return mid;
        } else if (arr[mid] < key) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1;
}

int main() {
    int n, key, result;
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int arr[n];
    printf("Enter the sorted elements: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Enter the element to search: ");
    scanf("%d", &key);

    result = binary_search(arr, n, key);
    if (result != -1) {
        printf("Element found at index %d\n", result);
    } else {
        printf("Element not found\n");
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:24:13

matrix_addition.c

                        #include <stdio.h>

int main() {
    int i, j, row, col;
    printf("Enter number of rows and columns: ");
    scanf("%d %d", &row, &col);

    int A[row][col], B[row][col], sum[row][col];

    printf("Enter elements of matrix A:\n");
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            scanf("%d", &A[i][j]);
        }
    }

    printf("Enter elements of matrix B:\n");
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            scanf("%d", &B[i][j]);
        }
    }

    // Matrix addition
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            sum[i][j] = A[i][j] + B[i][j];
        }
    }

    printf("Resultant matrix (A + B):\n");
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:24:30

matrix_multiplication.c

                        #include <stdio.h>

int main() {
    int i, j, k, row1, col1, row2, col2;
    printf("Enter number of rows and columns for matrix A: ");
    scanf("%d %d", &row1, &col1);
    printf("Enter number of rows and columns for matrix B: ");
    scanf("%d %d", &row2, &col2);

    if (col1 != row2) {
        printf("Matrix multiplication not possible\n");
        return 0;
    }

    int A[row1][col1], B[row2][col2], product[row1][col2];

    printf("Enter elements of matrix A:\n");
    for (i = 0; i < row1; i++) {
        for (j = 0; j < col1; j++) {
            scanf("%d", &A[i][j]);
        }
    }

    printf("Enter elements of matrix B:\n");
    for (i = 0; i < row2; i++) {
        for (j = 0; j < col2; j++) {
            scanf("%d", &B[i][j]);
        }
    }

    // Matrix multiplication
    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            product[i][j] = 0;
            for (k = 0; k < col1; k++) {
                product[i][j] += A[i][k] * B[k][j];
            }
        }
    }

    printf("Resultant matrix (A * B):\n");
    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            printf("%d ", product[i][j]);
        }
        printf("\n");
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:24:52

check_palindrome.c

                        #include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    int i, len, isPalindrome = 1;

    printf("Enter a string: ");
    scanf("%s", str);

    len = strlen(str);
    for (i = 0; i < len / 2; i++) {
        if (str[i] != str[len - i - 1]) {
            isPalindrome = 0;
            break;
        }
    }

    if (isPalindrome) {
        printf("The string is a palindrome\n");
    } else {
        printf("The string is not a palindrome\n");
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:25:08

reverse_string.c

                        #include <stdio.h>
#include <string.h>

int main() {
    char str[100], reversed[100];
    int i, len;

    printf("Enter a string: ");
    scanf("%s", str);

    len = strlen(str);
    for (i = 0; i < len; i++) {
        reversed[i] = str[len - i - 1];
    }
    reversed[len] = '\0'; // Null terminate the string

    printf("Reversed string: %s\n", reversed);
    return 0;
}                    

Last Modified: January 12, 2025 19:25:33

count_vowels_consonants.c

                        #include <stdio.h>
#include <ctype.h>

int main() {
    char str[100];
    int vowels = 0, consonants = 0, i;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (i = 0; str[i] != '\0'; i++) {
        if (isalpha(str[i])) {
            switch (tolower(str[i])) {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                    vowels++;
                    break;
                default:
                    consonants++;
                    break;
            }
        }
    }

    printf("Vowels: %d, Consonants: %d\n", vowels, consonants);
    return 0;
}                    

Last Modified: January 12, 2025 19:25:43

find_gcd_of_two_numbers.c

                        #include <stdio.h>

int gcd(int a, int b) {
    // Using Euclidean algorithm to find GCD
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    printf("The GCD of %d and %d is: %d\n", a, b, gcd(a, b));
    return 0;
}                    

Last Modified: January 12, 2025 19:27:19

find_lcm_of_two_numbers.c

                        #include <stdio.h>

int gcd(int a, int b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int lcm(int a, int b) {
    return (a * b) / gcd(a, b);
}

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    printf("The LCM of %d and %d is: %d\n", a, b, lcm(a, b));
    return 0;
}                    

Last Modified: January 12, 2025 19:27:35

generate_pascals_triangle.c

                        #include <stdio.h>

void printPascal(int n) {
    int i, j, num = 1;

    for (i = 0; i < n; i++) {
        num = 1;
        for (j = 0; j < (n - i - 1); j++) {
            printf(" "); // Print leading spaces
        }

        for (j = 0; j <= i; j++) {
            printf("%d ", num);
            num = num * (i - j) / (j + 1);
        }
        printf("\n");
    }
}

int main() {
    int n;
    printf("Enter the number of rows: ");
    scanf("%d", &n);

    printPascal(n);
    return 0;
}                    

Last Modified: January 12, 2025 19:27:52

tower_of_hanoi.c

                        #include <stdio.h>

void towerOfHanoi(int n, char from, char to, char aux) {
    if (n == 1) {
        printf("Move disk 1 from %c to %c\n", from, to);
        return;
    }
    towerOfHanoi(n - 1, from, aux, to);
    printf("Move disk %d from %c to %c\n", n, from, to);
    towerOfHanoi(n - 1, aux, to, from);
}

int main() {
    int n;
    printf("Enter the number of disks: ");
    scanf("%d", &n);

    towerOfHanoi(n, 'A', 'C', 'B');  // A is source, C is destination, B is auxiliary
    return 0;
}                    

Last Modified: January 12, 2025 19:28:09

calculate_power_using_recursion.c

                        #include <stdio.h>

int power(int base, int exp) {
    if (exp == 0) {
        return 1;
    }
    return base * power(base, exp - 1); // Recursively multiply the base
}

int main() {
    int base, exp;
    printf("Enter base and exponent: ");
    scanf("%d %d", &base, &exp);

    printf("%d raised to the power %d is: %d\n", base, exp, power(base, exp));
    return 0;
}                    

Last Modified: January 12, 2025 19:28:30

check_armstrong_number_using_functions.c

                        #include <stdio.h>
#include <math.h>

int isArmstrong(int num) {
    int sum = 0, temp = num, digit, n = 0;

    // Count number of digits in num
    while (temp != 0) {
        n++;
        temp /= 10;
    }

    temp = num;
    // Calculate sum of powers of digits
    while (temp != 0) {
        digit = temp % 10;
        sum += pow(digit, n);
        temp /= 10;
    }

    return sum == num;
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    if (isArmstrong(num)) {
        printf("%d is an Armstrong number\n", num);
    } else {
        printf("%d is not an Armstrong number\n", num);
    }
    return 0;
}                    

Last Modified: January 12, 2025 19:28:47

swap_two_numbers_using_pointers.c

                        #include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x, y;
    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);

    printf("Before swapping: x = %d, y = %d\n", x, y);
    swap(&x, &y);
    printf("After swapping: x = %d, y = %d\n", x, y);
    return 0;
}                    

Last Modified: January 12, 2025 19:29:11

reverse_array_using_pointers.c

                        #include <stdio.h>

void reverseArray(int *arr, int size) {
    int *start = arr;
    int *end = arr + size - 1;
    int temp;

    while (start < end) {
        temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}

int main() {
    int n;
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int arr[n];
    printf("Enter the elements: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    reverseArray(arr, n);

    printf("Reversed array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}                    

Last Modified: January 12, 2025 19:29:22

dynamic_memory_allocation_malloc_calloc_free.c

                        #include <stdio.h>
#include <stdlib.h>

int main() {
    int n;
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    // Dynamic memory allocation using malloc
    int *arr = (int *)malloc(n * sizeof(int));
    if (arr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    printf("Enter the elements: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Entered elements: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    // Free dynamically allocated memory
    free(arr);

    return 0;
}                    

Last Modified: January 12, 2025 19:29:42

pointer_to_a_function_example.c

                        #include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int main() {
    int (*funcPtr)(int, int);  // Function pointer declaration
    int num1, num2, result;

    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    // Pointing to add function
    funcPtr = &add;
    result = funcPtr(num1, num2);
    printf("Addition: %d\n", result);

    // Pointing to subtract function
    funcPtr = &subtract;
    result = funcPtr(num1, num2);
    printf("Subtraction: %d\n", result);

    return 0;
}                    

Last Modified: January 12, 2025 19:29:58

read_from_a_file.c

                        #include <stdio.h>

int main() {
    FILE *file;
    char ch;

    file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    printf("Contents of the file:\n");
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);  // Print each character
    }

    fclose(file);
    return 0;
}                    

Last Modified: January 12, 2025 19:31:42

write_to_a_file.c

                        #include <stdio.h>

int main() {
    FILE *file;
    char str[] = "Hello, this is a test message written to a file.";

    file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    fprintf(file, "%s\n", str);
    printf("Data successfully written to file.\n");

    fclose(file);
    return 0;
}                    

Last Modified: January 12, 2025 19:31:58

copy_a_file.c

                        #include <stdio.h>

int main() {
    FILE *source, *destination;
    char ch;

    source = fopen("source.txt", "r");
    if (source == NULL) {
        printf("Error opening source file.\n");
        return 1;
    }

    destination = fopen("destination.txt", "w");
    if (destination == NULL) {
        printf("Error opening destination file.\n");
        fclose(source);
        return 1;
    }

    while ((ch = fgetc(source)) != EOF) {
        fputc(ch, destination);
    }

    printf("File copied successfully.\n");

    fclose(source);
    fclose(destination);
    return 0;
}                    

Last Modified: January 12, 2025 19:32:15

append_data_to_a_file.c

                        #include <stdio.h>

int main() {
    FILE *file;
    char str[] = "This is an appended line of text.";

    file = fopen("example.txt", "a");
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    fprintf(file, "%s\n", str);
    printf("Data successfully appended to file.\n");

    fclose(file);
    return 0;
}                    

Last Modified: January 12, 2025 19:32:31

count_words_in_a_file.c

                        #include <stdio.h>
#include <ctype.h>

int main() {
    FILE *file;
    char ch;
    int words = 0;
    int in_word = 0;

    file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    while ((ch = fgetc(file)) != EOF) {
        if (isspace(ch)) {
            in_word = 0;
        } else if (in_word == 0) {
            in_word = 1;
            words++;
        }
    }

    printf("Word count: %d\n", words);

    fclose(file);
    return 0;
}                    

Last Modified: January 12, 2025 19:32:49

implement_a_linked_list_singly.c

                        #include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

void printList(struct Node *head) {
    struct Node *temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node *head = NULL;
    struct Node *second = NULL;
    struct Node *third = NULL;

    head = (struct Node *)malloc(sizeof(struct Node));
    second = (struct Node *)malloc(sizeof(struct Node));
    third = (struct Node *)malloc(sizeof(struct Node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    printf("Linked List: ");
    printList(head);

    return 0;
}                    

Last Modified: January 12, 2025 19:33:11

implement_a_linked_list_doubly.c

                        #include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *prev;
    struct Node *next;
};

void printList(struct Node *head) {
    struct Node *temp = head;
    while (temp != NULL) {
        printf("%d <-> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node *head = NULL;
    struct Node *second = NULL;
    struct Node *third = NULL;

    head = (struct Node *)malloc(sizeof(struct Node));
    second = (struct Node *)malloc(sizeof(struct Node));
    third = (struct Node *)malloc(sizeof(struct Node));

    head->data = 1;
    head->prev = NULL;
    head->next = second;

    second->data = 2;
    second->prev = head;
    second->next = third;

    third->data = 3;
    third->prev = second;
    third->next = NULL;

    printf("Doubly Linked List: ");
    printList(head);

    return 0;
}                    

Last Modified: January 12, 2025 19:33:50

implement_a_stack_using_arrays.c

                        #include <stdio.h>
#define MAX 5

int stack[MAX];
int top = -1;

int isFull() {
    return top == MAX - 1;
}

int isEmpty() {
    return top == -1;
}

void push(int value) {
    if (isFull()) {
        printf("Stack Overflow\n");
    } else {
        stack[++top] = value;
        printf("%d pushed to stack\n", value);
    }
}

int pop() {
    if (isEmpty()) {
        printf("Stack Underflow\n");
        return -1;
    } else {
        return stack[top--];
    }
}

int peek() {
    if (isEmpty()) {
        printf("Stack is empty\n");
        return -1;
    } else {
        return stack[top];
    }
}

int main() {
    push(10);
    push(20);
    push(30);
    printf("Top element is: %d\n", peek());
    printf("%d popped from stack\n", pop());
    printf("Top element is now: %d\n", peek());

    return 0;
}                    

Last Modified: January 12, 2025 19:34:09

implement_a_queue_using_arrays.c

                        #include <stdio.h>
#define MAX 5

int queue[MAX];
int front = -1, rear = -1;

int isFull() {
    return (rear + 1) % MAX == front;
}

int isEmpty() {
    return front == -1;
}

void enqueue(int value) {
    if (isFull()) {
        printf("Queue Overflow\n");
    } else {
        if (front == -1) {
            front = 0;
        }
        rear = (rear + 1) % MAX;
        queue[rear] = value;
        printf("%d enqueued to queue\n", value);
    }
}

int dequeue() {
    if (isEmpty()) {
        printf("Queue Underflow\n");
        return -1;
    } else {
        int item = queue[front];
        if (front == rear) {
            front = rear = -1;
        } else {
            front = (front + 1) % MAX;
        }
        return item;
    }
}

int main() {
    enqueue(10);
    enqueue(20);
    enqueue(30);
    printf("%d dequeued from queue\n", dequeue());
    printf("%d dequeued from queue\n", dequeue());
    return 0;
}                    

Last Modified: January 12, 2025 19:34:26

binary_search_tree_operations.c

                        #include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *left, *right;
};

struct Node* newNode(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}

struct Node* insert(struct Node* root, int data) {
    if (root == NULL)
        return newNode(data);

    if (data < root->data)
        root->left = insert(root->left, data);
    else
        root->right = insert(root->right, data);

    return root;
}

struct Node* minValueNode(struct Node* node) {
    struct Node* current = node;
    while (current && current->left != NULL)
        current = current->left;

    return current;
}

struct Node* deleteNode(struct Node* root, int key) {
    if (root == NULL)
        return root;

    if (key < root->data)
        root->left = deleteNode(root->left, key);
    else if (key > root->data)
        root->right = deleteNode(root->right, key);
    else {
        if (root->left == NULL) {
            struct Node* temp = root->right;
            free(root);
            return temp;
        } else if (root->right == NULL) {
            struct Node* temp = root->left;
            free(root);
            return temp;
        }

        struct Node* temp = minValueNode(root->right);
        root->data = temp->data;
        root->right = deleteNode(root->right, temp->data);
    }

    return root;
}

void inorder(struct Node* root) {
    if (root != NULL) {
        inorder(root->left);
        printf("%d ", root->data);
        inorder(root->right);
    }
}

int main() {
    struct Node* root = NULL;

    root = insert(root, 50);
    root = insert(root, 30);
    root = insert(root, 20);
    root = insert(root, 40);
    root = insert(root, 70);
    root = insert(root, 60);
    root = insert(root, 80);

    printf("Inorder traversal: ");
    inorder(root);
    printf("\n");

    root = deleteNode(root, 20);
    printf("Inorder traversal after deletion: ");
    inorder(root);
    printf("\n");

    return 0;
}                    

Last Modified: January 12, 2025 19:34:44

dijkstras_algorithm.c

                        #include <stdio.h>
#include <limits.h>

#define V 9

int minDistance(int dist[], int sptSet[]) {
    int min = INT_MAX, min_index;

    for (int v = 0; v < V; v++) {
        if (sptSet[v] == 0 && dist[v] <= min) {
            min = dist[v];
            min_index = v;
        }
    }

    return min_index;
}

void dijkstra(int graph[V][V], int src) {
    int dist[V];
    int sptSet[V];  // Shortest Path Tree Set

    for (int i = 0; i < V; i++) {
        dist[i] = INT_MAX;
        sptSet[i] = 0;
    }

    dist[src] = 0;

    for (int count = 0; count < V - 1; count++) {
        int u = minDistance(dist, sptSet);

        sptSet[u] = 1;

        for (int v = 0; v < V; v++) {
            if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
                dist[v] = dist[u] + graph[u][v];
            }
        }
    }

    printf("Vertex\tDistance from Source\n");
    for (int i = 0; i < V; i++) {
        printf("%d\t%d\n", i, dist[i]);
    }
}

int main() {
    int graph[V][V] = { {0, 4, 0, 0, 0, 0, 0, 8, 0},
                        {4, 0, 8, 0, 0, 0, 0, 11, 0},
                        {0, 8, 0, 7, 0, 4, 0, 0, 2},
                        {0, 0, 7, 0, 9, 14, 0, 0, 0},
                        {0, 0, 0, 9, 0, 10, 0, 0, 0},
                        {0, 0, 4, 14, 10, 0, 2, 0, 0},
                        {0, 0, 0, 0, 0, 2, 0, 1, 6},
                        {8, 11, 0, 0, 0, 0, 1, 0, 7},
                        {0, 0, 2, 0, 0, 0, 6, 7, 0} };

    dijkstra(graph, 0);  // Starting from vertex 0

    return 0;
}                    

Last Modified: January 12, 2025 19:35:08

ADVERTISEMENT