Top 100 C Programs for Interview

1. Hello World:

#include <stdio.h>

 

int main() {

    printf("Hello, World!\n");

    return 0;

}

2. Input and Output:

#include <stdio.h>

 

int main() {

    int num;

    printf("Enter a number: ");

    scanf("%d", &num);

    printf("You entered: %d\n", num);

    return 0;

}

3. Swapping Two Numbers:

#include <stdio.h>

 

int main() {

    int a, b, temp;

    printf("Enter two numbers: ");

    scanf("%d %d", &a, &b);

    temp = a;

    a = b;

    b = temp;

    printf("After swapping: a = %d, b = %d\n", a, b);

    return 0;

}

4. Finding Maximum and Minimum:

#include <stdio.h>

 

int main() {

    int num1, num2;

    printf("Enter two numbers: ");

    scanf("%d %d", &num1, &num2);

   

    if (num1 > num2) {

        printf("Maximum: %d\n", num1);

        printf("Minimum: %d\n", num2);

    } else {

        printf("Maximum: %d\n", num2);

        printf("Minimum: %d\n", num1);

    }

   

    return 0;

}

5. Factorial of a Number:

#include <stdio.h>

 

int main() {

    int num, factorial = 1;

    printf("Enter a number: ");

    scanf("%d", &num);

   

    for (int i = 1; i <= num; ++i) {

        factorial *= i;

    }

   

    printf("Factorial of %d is %d\n", num, factorial);

    return 0;

}

6. Prime Number Check:

#include <stdio.h>

#include <stdbool.h>

 

bool isPrime(int num) {

    if (num <= 1) {

        return false;

    }

   

    for (int i = 2; i * i <= num; ++i) {

        if (num % i == 0) {

            return false;

        }

    }

   

    return true;

}

 

int main() {

    int num;

    printf("Enter a number: ");

    scanf("%d", &num);

   

    if (isPrime(num)) {

        printf("%d is a prime number.\n", num);

    } else {

        printf("%d is not a prime number.\n", num);

    }

   

    return 0;

}

7. Fibonacci Series:

#include <stdio.h>

 

int main() {

    int n, first = 0, second = 1, next;

    printf("Enter the number of terms: ");

    scanf("%d", &n);

   

    printf("Fibonacci Series: ");

    for (int i = 0; i < n; ++i) {

        printf("%d, ", first);

        next = first + second;

        first = second;

        second = next;

    }

   

    printf("\n");

    return 0;

}

8. Palindrome Check:

#include <stdio.h>

#include <stdbool.h>

#include <string.h>

 

bool isPalindrome(char str[]) {

    int left = 0;

    int right = strlen(str) - 1;

   

    while (left < right) {

        if (str[left] != str[right]) {

            return false;

        }

        left++;

        right--;

    }

   

    return true;

}

 

int main() {

    char str[100];

    printf("Enter a string: ");

    scanf("%s", str);

   

    if (isPalindrome(str)) {

        printf("%s is a palindrome.\n", str);

    } else {

        printf("%s is not a palindrome.\n", str);

    }

   

    return 0;

}

9. Reverse a String:

#include <stdio.h>

#include <string.h>

 

void reverseString(char str[]) {

    int length = strlen(str);

    for (int i = 0; i < length / 2; ++i) {

        char temp = str[i];

        str[i] = str[length - i - 1];

        str[length - i - 1] = temp;

    }

}

 

int main() {

    char str[100];

    printf("Enter a string: ");

    scanf("%s", str);

   

    reverseString(str);

    printf("Reversed string: %s\n", str);

   

    return 0;

}

10. Sum of Digits:

#include <stdio.h>

 

int main() {

    int num, originalNum, remainder, sum = 0;

    printf("Enter a number: ");

    scanf("%d", &num);

   

    originalNum = num;

    while (num > 0) {

        remainder = num % 10;

        sum += remainder;

        num /= 10;

    }

   

    printf("Sum of digits of %d is %d\n", originalNum, sum);

    return 0;

}

11. Armstrong Number Check:

#include <stdio.h>

#include <math.h>

 

int main() {

    int num, originalNum, remainder, n = 0, sum = 0;

    printf("Enter a number: ");

    scanf("%d", &num);

   

    originalNum = num;

    

    // Count the number of digits

    while (originalNum != 0) {

        originalNum /= 10;

        ++n;

    }

   

    originalNum = num;

   

    // Calculate the sum of nth power of digits

    while (originalNum != 0) {

        remainder = originalNum % 10;

        sum += pow(remainder, n);

        originalNum /= 10;

    }

   

    if (sum == num) {

        printf("%d is an Armstrong number.\n", num);

    } else {

        printf("%d is not an Armstrong number.\n", num);

    }

   

    return 0;

}

12. Check Leap Year:

#include <stdio.h>

#include <stdbool.h>

 

bool isLeapYear(int year) {

    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

        return true;

    }

    return false;

}

 

int main() {

    int year;

    printf("Enter a year: ");

    scanf("%d", &year);

   

    if (isLeapYear(year)) {

        printf("%d is a leap year.\n", year);

    } else {

        printf("%d is not a leap year.\n", year);

    }

   

    return 0;

}

13 .Linear Search:

#include <stdio.h>

 

int linearSearch(int arr[], int n, int target) {

    for (int i = 0; i < n; i++) {

        if (arr[i] == target) {

            return i; // Found the target at index i

        }

    }

    return -1; // Target not found

}

 

int main() {

    int arr[] = {2, 4, 6, 8, 10, 12};

    int n = sizeof(arr) / sizeof(arr[0]);

    int target = 8;

 

    int index = linearSearch(arr, n, target);

 

    if (index != -1) {

        printf("Target found at index %d\n", index);

    } else {

        printf("Target not found\n");

    }

 

    return 0;

}

14.Binary Search:

#include <stdio.h>

 

int binarySearch(int arr[], int low, int high, int target) {

    while (low <= high) {

        int mid = low + (high - low) / 2;

        if (arr[mid] == target) {

            return mid; // Found the target at index mid

        } else if (arr[mid] < target) {

            low = mid + 1;

        } else {

            high = mid - 1;

        }

    }

    return -1; // Target not found

}

 

int main() {

    int arr[] = {2, 4, 6, 8, 10, 12};

    int n = sizeof(arr) / sizeof(arr[0]);

    int target = 8;

 

    int index = binarySearch(arr, 0, n - 1, target);

 

    if (index != -1) {

        printf("Target found at index %d\n", index);

    } else {

        printf("Target not found\n");

    }

 

    return 0;

}

15.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]) {

                // Swap arr[j] and 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 = sizeof(arr) / sizeof(arr[0]);

 

    bubbleSort(arr, n);

 

    printf("Sorted array: ");

    for (int i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

 

    return 0;

}

16.Selection Sort:

#include <stdio.h>

 

void selectionSort(int arr[], int n) {

    for (int i = 0; i < n - 1; i++) {

        int minIndex = i;

        for (int j = i + 1; j < n; j++) {

            if (arr[j] < arr[minIndex]) {

                minIndex = j;

            }

        }

        // Swap arr[i] and arr[minIndex]

        int temp = arr[i];

        arr[i] = arr[minIndex];

        arr[minIndex] = temp;

    }

}

 

int main() {

    int arr[] = {64, 34, 25, 12, 22, 11, 90};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    selectionSort(arr, n);

 

    printf("Sorted array: ");

    for (int i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

 

    return 0;

}

17.Insertion Sort:

#include <stdio.h>

 

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;

    }

}

 

int main() {

    int arr[] = {64, 34, 25, 12, 22, 11, 90};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    insertionSort(arr, n);

 

    printf("Sorted array: ");

    for (int i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

 

    return 0;

}

18.Merge Sort:

#include <stdio.h>

 

void merge(int arr[], int left, int middle, int right) {

    int n1 = middle - left + 1;

    int n2 = right - middle;

 

    int L[n1], R[n2];

 

    for (int i = 0; i < n1; i++) {

        L[i] = arr[left + i];

    }

    for (int j = 0; j < n2; j++) {

        R[j] = arr[middle + 1 + j];

    }

 

    int i = 0, j = 0, k = left;

 

    while (i < n1 && j < n2) {

        if (L[i] <= R[j]) {

            arr[k] = L[i];

            i++;

        } else {

            arr[k] = R[j];

            j++;

        }

        k++;

    }

 

    while (i < n1) {

        arr[k] = L[i];

        i++;

        k++;

    }

 

    while (j < n2) {

        arr[k] = R[j];

        j++;

        k++;

    }

}

 

void mergeSort(int arr[], int left, int right) {

    if (left < right) {

        int middle = left + (right - left) / 2;

 

        mergeSort(arr, left, middle);

        mergeSort(arr, middle + 1, right);

 

        merge(arr, left, middle, right);

    }

}

 

int main() {

    int arr[] = {12, 11, 13, 5, 6, 7};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    mergeSort(arr, 0, n - 1);

 

    printf("Sorted array: ");

    for (int i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

 

    return 0;

}

19.Quick Sort:

#include <stdio.h>

 

void swap(int* a, int* b) {

    int t = *a;

    *a = *b;

    *b = t;

}

 

int partition(int arr[], int low, int

 

 high) {

    int pivot = arr[high];

    int i = (low - 1);

 

    for (int j = low; j <= high - 1; j++) {

        if (arr[j] < pivot) {

            i++;

            swap(&arr[i], &arr[j]);

        }

    }

    swap(&arr[i + 1], &arr[high]);

    return (i + 1);

}

 

void quickSort(int arr[], int low, int high) {

    if (low < high) {

        int pi = partition(arr, low, high);

 

        quickSort(arr, low, pi - 1);

        quickSort(arr, pi + 1, high);

    }

}

 

int main() {

    int arr[] = {10, 7, 8, 9, 1, 5};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    quickSort(arr, 0, n - 1);

 

    printf("Sorted array: ");

    for (int i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

 

    return 0;

}

20.Counting Sort:

#include <stdio.h>

 

void countingSort(int arr[], int n, int range) {

    int output[n];

    int count[range + 1];

    for (int i = 0; i <= range; i++) {

        count[i] = 0;

    }

 

    for (int i = 0; i < n; i++) {

        count[arr[i]]++;

    }

 

    for (int i = 1; i <= range; i++) {

        count[i] += count[i - 1];

    }

 

    for (int i = n - 1; i >= 0; i--) {

        output[count[arr[i]] - 1] = arr[i];

        count[arr[i]]--;

    }

 

    for (int i = 0; i < n; i++) {

        arr[i] = output[i];

    }

}

 

int main() {

    int arr[] = {4, 2, 2, 8, 3, 3, 1};

    int n = sizeof(arr) / sizeof(arr[0]);

    int range = 8;

 

    countingSort(arr, n, range);

 

    printf("Sorted array: ");

    for (int i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

 

    return 0;

}

21.Finding Duplicate Elements:

#include <stdio.h>

 

void findDuplicates(int arr[], int n) {

    for (int i = 0; i < n - 1; i++) {

        for (int j = i + 1; j < n; j++) {

            if (arr[i] == arr[j]) {

                printf("Duplicate element: %d\n", arr[i]);

            }

        }

    }

}

 

int main() {

    int arr[] = {2, 4, 6, 8, 10, 6};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    findDuplicates(arr, n);

 

    return 0;

}

22.Finding Missing Elements:

#include <stdio.h>

 

void findMissingElements(int arr[], int n, int range) {

    int hash[range + 1];

    for (int i = 0; i <= range; i++) {

        hash[i] = 0;

    }

 

    for (int i = 0; i < n; i++) {

        hash[arr[i]] = 1;

    }

 

    printf("Missing elements: ");

    for (int i = 1; i <= range; i++) {

        if (hash[i] == 0) {

            printf("%d ", i);

        }

    }

    printf("\n");

}

 

int main() {

    int arr[] = {3, 7, 1, 2, 8, 4};

    int n = sizeof(arr) / sizeof(arr[0]);

    int range = 8;

 

    findMissingElements(arr, n, range);

 

    return 0;

}

23.Reversing an Array:

#include <stdio.h>

 

void reverseArray(int arr[], int n) {

    int start = 0;

    int end = n - 1;

   

    while (start < end) {

        int temp = arr[start];

        arr[start] = arr[end];

        arr[end] = temp;

       

        start++;

        end--;

    }

}

 

int main() {

    int arr[] = {1, 2, 3, 4, 5};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    reverseArray(arr, n);

 

    printf("Reversed array: ");

    for (int i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

 

    return 0;

}

24.Finding the Largest Element:

#include <stdio.h>

 

int findLargestElement(int arr[], int n) {

    int maxElement = arr[0];

   

    for (int i = 1; i < n; i++) {

        if (arr[i] > maxElement) {

            maxElement = arr[i];

        }

    }

   

    return maxElement;

}

 

int main() {

    int arr[] = {12, 35, 1, 10, 34, 1};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    int largest = findLargestElement(arr, n);

 

    printf("Largest element: %d\n", largest);

 

    return 0;

}

26.Finding the Second Largest Element:

#include <stdio.h>

 

void findTwoLargest(int arr[], int n) {

    int firstLargest, secondLargest;

   

    if (arr[0] > arr[1]) {

        firstLargest = arr[0];

        secondLargest = arr[1];

    } else {

        firstLargest = arr[1];

        secondLargest = arr[0];

    }

   

    for (int i = 2; i < n; i++) {

        if (arr[i] > firstLargest) {

            secondLargest = firstLargest;

            firstLargest = arr[i];

        } else if (arr[i] > secondLargest && arr[i] != firstLargest) {

            secondLargest = arr[i];

        }

    }

   

    printf("First largest: %d\n", firstLargest);

    printf("Second largest: %d\n", secondLargest);

}

 

int main() {

    int arr[] = {12, 35, 1, 10, 34, 6};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    findTwoLargest(arr, n);

 

    return 0;

}

27.Finding Common Elements in Two Arrays:

#include <stdio.h>

 

void findCommonElements(int arr1[], int arr2[], int n1, int n2) {

    printf("Common elements: ");

   

    for (int i =

 

0; i < n1; i++) {

        for (int j = 0; j < n2; j++) {

            if (arr1[i] == arr2[j]) {

                printf("%d ", arr1[i]);

                break;

            }

        }

    }

   

    printf("\n");

}

 

int main() {

    int arr1[] = {1, 2, 4, 5, 6};

    int arr2[] = {2, 3, 5, 7};

    int n1 = sizeof(arr1) / sizeof(arr1[0]);

    int n2 = sizeof(arr2) / sizeof(arr2[0]);

 

    findCommonElements(arr1, arr2, n1, n2);

 

    return 0;

}

28.Finding Subarray with Maximum Sum:

#include <stdio.h>

 

int maxSubarraySum(int arr[], int n) {

    int maxSoFar = arr[0];

    int maxEndingHere = arr[0];

   

    for (int i = 1; i < n; i++) {

        maxEndingHere = maxEndingHere + arr[i];

        if (maxEndingHere < arr[i]) {

            maxEndingHere = arr[i];

        }

        if (maxSoFar < maxEndingHere) {

            maxSoFar = maxEndingHere;

        }

    }

   

    return maxSoFar;

}

 

int main() {

    int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    int maxSum = maxSubarraySum(arr, n);

 

    printf("Maximum subarray sum: %d\n", maxSum);

 

    return 0;

}

29.Finding Subarray with Given Sum:

#include <stdio.h>

 

void subarrayWithGivenSum(int arr[], int n, int targetSum) {

    int currentSum = arr[0];

    int start = 0;

 

    for (int end = 1; end <= n; end++) {

        while (currentSum > targetSum && start < end - 1) {

            currentSum -= arr[start];

            start++;

        }

 

        if (currentSum == targetSum) {

            printf("Subarray with sum %d found between indexes %d and %d\n", targetSum, start, end - 1);

            return;

        }

 

        if (end < n) {

            currentSum += arr[end];

        }

    }

 

    printf("No subarray found with sum %d\n", targetSum);

}

 

int main() {

    int arr[] = {1, 4, 20, 3, 10, 5};

    int n = sizeof(arr) / sizeof(arr[0]);

    int targetSum = 33;

 

    subarrayWithGivenSum(arr, n, targetSum);

 

    return 0;

}

30.Finding Subarray with Zero Sum:

#include <stdio.h>

 

int subarrayWithZeroSum(int arr[], int n) {

    int sum = 0;

    for (int i = 0; i < n; i++) {

        sum += arr[i];

        if (sum == 0 || arr[i] == 0) {

            return 1;

        }

    }

    return 0;

}

 

int main() {

    int arr[] = {4, 2, -3, 1, 6};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    if (subarrayWithZeroSum(arr, n)) {

        printf("Subarray with zero sum found\n");

    } else {

        printf("No subarray with zero sum\n");

    }

 

    return 0;

}

31.String Length:

#include <stdio.h>

 

int stringLength(const char* str) {

    int length = 0;

    while (str[length] != '\0') {

        length++;

    }

    return length;

}

 

int main() {

    const char* str = "Hello, World!";

    int length = stringLength(str);

    printf("Length of the string: %d\n", length);

 

    return 0;

}

32.String Copy:

#include <stdio.h>

 

void stringCopy(char* dest, const char* src) {

    int i = 0;

    while (src[i] != '\0') {

        dest[i] = src[i];

        i++;

    }

    dest[i] = '\0';

}

 

int main() {

    const char* src = "Hello, World!";

    char dest[50];

 

    stringCopy(dest, src);

 

    printf("Copied string: %s\n", dest);

 

    return 0;

}

33.String Concatenation:

#include <stdio.h>

 

void stringConcatenate(char* dest, const char* src) {

    int destLength = 0;

    while (dest[destLength] != '\0') {

        destLength++;

    }

 

    int i = 0;

    while (src[i] != '\0') {

        dest[destLength + i] = src[i];

        i++;

    }

    dest[destLength + i] = '\0';

}

 

int main() {

    char dest[50] = "Hello, ";

    const char* src = "World!";

 

    stringConcatenate(dest, src);

 

    printf("Concatenated string: %s\n", dest);

 

    return 0;

}

34.String Comparison:

#include <stdio.h>

 

int stringCompare(const char* str1, const char* str2) {

    int i = 0;

    while (str1[i] == str2[i]) {

        if (str1[i] == '\0') {

            return 0; // Strings are equal

        }

        i++;

    }

    return str1[i] - str2[i];

}

 

int main() {

    const char* str1 = "apple";

    const char* str2 = "banana";

 

    int result = stringCompare(str1, str2);

 

    if (result == 0) {

        printf("Strings are equal\n");

    } else if (result < 0) {

        printf("String 1 is lexicographically smaller\n");

    } else {

        printf("String 2 is lexicographically smaller\n");

    }

 

    return 0;

}

36.String Reversal:

#include <stdio.h>

 

void stringReverse(char* str) {

    int length = 0;

    while (str[length] != '\0') {

        length++;

    }

 

    int start = 0;

    int end = length - 1;

 

    while (start < end) {

        char temp = str[start];

        str[start] = str[end];

        str[end] = temp;

 

        start++;

        end--;

    }

}

 

int main() {

    char str[] = "Hello, World!";

   

    stringReverse(str);

 

    printf("Reversed string: %s\n", str);

 

    return 0;

}

37.Palindrome String Check:

#include <stdio.h>

#include <stdbool.h>

 

bool isPalindrome(const char* str) {

    int start =

 

0;

    int end = 0;

    while (str[end] != '\0') {

        end++;

    }

    end--;

 

    while (start < end) {

        if (str[start] != str[end]) {

            return false;

        }

        start++;

        end--;

    }

    return true;

}

 

int main() {

    const char* str = "racecar";

 

    if (isPalindrome(str)) {

        printf("The string is a palindrome\n");

    } else {

        printf("The string is not a palindrome\n");

    }

 

    return 0;

}

38.Counting Vowels and Consonants:

#include <stdio.h>

#include <ctype.h>

 

void countVowelsAndConsonants(const char* str, int* vowels, int* consonants) {

    *vowels = 0;

    *consonants = 0;

 

    for (int i = 0; str[i] != '\0'; i++) {

        char ch = tolower(str[i]);

        if (ch >= 'a' && ch <= 'z') {

            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

                (*vowels)++;

            } else {

                (*consonants)++;

            }

        }

    }

}

 

int main() {

    const char* str = "Hello, World!";

    int vowels, consonants;

 

    countVowelsAndConsonants(str, &vowels, &consonants);

 

    printf("Vowels: %d\n", vowels);

    printf("Consonants: %d\n", consonants);

 

    return 0;

}

39.Counting Words in a String:

#include <stdio.h>

#include <stdbool.h>

#include <ctype.h>

 

bool isWhitespace(char ch) {

    return (ch == ' ' || ch == '\t' || ch == '\n');

}

 

int countWords(const char* str) {

    int wordCount = 0;

    bool inWord = false;

 

    for (int i = 0; str[i] != '\0'; i++) {

        if (!isWhitespace(str[i])) {

            if (!inWord) {

                inWord = true;

                wordCount++;

            }

        } else {

            inWord = false;

        }

    }

 

    return wordCount;

}

 

int main() {

    const char* str = "This is a sample sentence with several words.";

    int words = countWords(str);

 

    printf("Number of words: %d\n", words);

 

    return 0;

}

Absolutely, here’s the complete C code for each of the programs you’ve listed:

38. Pointer Basics:

#include <stdio.h>

 

int main() {

    int num = 10;

    int *ptr; // Declaration of a pointer

   

    ptr = &num; // Initializing the pointer with the address of 'num'

   

    printf("Value of num: %d\n", num);

    printf("Address of num: %p\n", &num);

    printf("Value of ptr: %p\n", ptr);

    printf("Value pointed by ptr: %d\n", *ptr); // Dereferencing the pointer

   

    return 0;

}

39. Swapping Using Pointers:

#include <stdio.h>

 

void swap(int *a, int *b) {

    int temp = *a;

    *a = *b;

    *b = temp;

}

 

int main() {

    int x = 5, y = 10;

   

    printf("Before swapping: x = %d, y = %d\n", x, y);

   

    swap(&x, &y); // Passing pointers to the variables

   

    printf("After swapping: x = %d, y = %d\n", x, y);

   

    return 0;

}

40. Passing Arrays to Functions:

#include <stdio.h>

 

void printArray(int arr[], int size) {

    for (int i = 0; i < size; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

}

 

int main() {

    int arr[] = {1, 2, 3, 4, 5};

    int size = sizeof(arr) / sizeof(arr[0]);

   

    printf("Array elements before function call: ");

    printArray(arr, size);

   

    // Pass the array to the function

    printf("Array elements after function call: ");

    printArray(arr, size);

   

    return 0;

}

41. Returning Pointers from Functions:

#include <stdio.h>

 

int *findMax(int arr[], int size) {

    int max = arr[0];

    int *maxPtr = &arr[0];

   

    for (int i = 1; i < size; i++) {

        if (arr[i] > max) {

            max = arr[i];

            maxPtr = &arr[i];

        }

    }

   

    return maxPtr;

}

 

int main() {

    int arr[] = {45, 78, 23, 56, 89};

    int size = sizeof(arr) / sizeof(arr[0]);

   

    int *maxPtr = findMax(arr, size);

   

    printf("Maximum value: %d\n", *maxPtr);

   

    return 0;

}

42. Dynamic Memory Allocation (malloc, calloc, realloc, free):

#include <stdio.h>

#include <stdlib.h>

 

int main() {

    int n;

    printf("Enter the number of elements: ");

    scanf("%d", &n);

 

    // Allocate memory using malloc

    int *arr1 = (int *)malloc(n * sizeof(int));

    if (arr1 == NULL) {

        printf("Memory allocation failed.\n");

        return 1;

    }

 

    // Allocate memory using calloc

    int *arr2 = (int *)calloc(n, sizeof(int));

    if (arr2 == NULL) {

        printf("Memory allocation failed.\n");

        return 1;

    }

 

    // Deallocate memory

    free(arr1);

    free(arr2);

 

    return 0;

}

43. Linked List Creation and Traversal:

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node *next;

};

 

int main() {

    struct Node *head = NULL;  // Initialize an empty linked list

   

    // Creating nodes

    struct Node *node1 = (struct Node *)malloc(sizeof(struct Node));

    struct Node *node2 = (struct Node *)malloc(sizeof(struct Node));

    struct Node *node3 = (struct Node *)malloc(sizeof(struct Node));

   

    node1->data = 1;

    node1->next = node2;

   

    node2->data = 2;

    node2->next = node3;

   

    node3->data = 3;

    node3->next = NULL;

   

    head = node1; // Set the head of the linked list

   

    // Traversing the linked list

    struct Node *current = head;

    while (current != NULL) {

        printf("%d ", current->data);

        current = current->next;

    }

   

    // Free memory

    free(node1);

    free(node2);

    free(node3);

   

    return 0;

}

44. Finding Length of Linked List:

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node *next;

};

 

int findLength(struct Node *head) {

    int length = 0;

    struct Node *current = head;

    while (current != NULL) {

        length++;

        current = current->next;

    }

    return length;

}

 

int main() {

    struct Node *head = NULL;

   

    // Code to create the linked list...

   

    int length = findLength(head);

    printf("Length of the linked list: %d\n", length);

   

    // Code to free memory...

   

    return 0;

}

45. Inserting a Node in a Linked List:

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node *next;

};

 

void insertNode(struct Node **head, int newData) {

    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

    newNode->data = newData;

    newNode->next = *head;

    *head = newNode;

}

 

int main() {

    struct Node *head = NULL;

   

    // Code to create the linked list...

   

    int newData = 42;

    insertNode(&head, newData);

   

    // Code to traverse and free memory...

   

    return 0;

}

46. Deleting a Node from a Linked List:

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node *next;

};

 

void deleteNode(struct Node **head, int key) {

    struct Node *temp = *head, *prev = NULL;

   

    if (temp != NULL && temp->data == key) {

        *head = temp->next;

        free(temp);

        return;

    }

   

    while (temp != NULL && temp->data != key) {

        prev = temp;

        temp = temp->next;

    }

   

    if (temp == NULL) return;

   

    prev->next = temp->next;

    free(temp);

}

 

int main() {

    struct Node *head = NULL;

   

    // Code to create the linked list...

   

    int keyToDelete = 42;

    deleteNode(&head, keyToDelete);

   

    // Code to traverse and free memory...

   

    return 0;

}

  1. Reversing a Linked List:

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node *next;

};

 

void reverseList(struct Node **head) {

    struct Node *prev = NULL, *current = *head, *nextNode = NULL;

    while (current != NULL) {

        nextNode = current->next;

        current->next = prev;

        prev = current;

        current = nextNode;

    }

    *head = prev;

}

 

void printList(struct Node *head) {

    struct Node *current = head;

    while (current != NULL) {

        printf("%d ", current->data);

        current = current->next;

    }

    printf("\n");

}

 

int main() {

    struct Node *head = NULL;

    struct Node *node1 = (struct Node *)malloc(sizeof(struct Node));

    struct Node *node2 = (struct Node *)malloc(sizeof(struct Node));

    struct Node *node3 = (struct Node *)malloc(sizeof(struct Node));

   

    node1->data = 1;

    node1->next = node2;

   

    node2->data = 2;

    node2->next = node3;

   

    node3->data = 3;

    node3->next = NULL;

   

    head = node1;

   

    printf("Original linked list: ");

    printList(head);

   

    reverseList(&head);

   

    printf("Reversed linked list: ");

    printList(head);

   

    // Free memory

    free(node1);

    free(node2);

    free(node3);

   

    return 0;

}

.

48. Creating and Accessing Structures:

#include <stdio.h>

 

struct Student {

    int rollNumber;

    char name[50];

    float marks;

};

 

int main() {

    struct Student student1;

   

    student1.rollNumber = 101;

    strcpy(student1.name, "John");

    student1.marks = 85.5;

   

    printf("Roll Number: %d\n", student1.rollNumber);

    printf("Name: %s\n", student1.name);

    printf("Marks: %.2f\n", student1.marks);

   

    return 0;

}

49. Nested Structures:

#include <stdio.h>

 

struct Address {

    char street[50];

    char city[50];

    char state[50];

};

 

struct Person {

    char name[50];

    int age;

    struct Address address;

};

 

int main() {

    struct Person person1;

   

    strcpy(person1.name, "Alice");

    person1.age = 25;

    strcpy(person1.address.street, "123 Main St");

    strcpy(person1.address.city, "Anytown");

    strcpy(person1.address.state, "CA");

   

    printf("Name: %s\n", person1.name);

    printf("Age: %d\n", person1.age);

    printf("Address: %s, %s, %s\n", person1.address.street, person1.address.city, person1.address.state);

   

    return 0;

}

50. Array of Structures:

#include <stdio.h>

 

struct Employee {

    int empId;

    char name[50];

    float salary;

};

 

int main() {

    struct Employee employees[3];

   

    for (int i = 0; i < 3; i++) {

        printf("Enter details for employee %d:\n", i + 1);

        printf("Employee ID: ");

        scanf("%d", &employees[i].empId);

        printf("Name: ");

        scanf("%s", employees[i].name);

        printf("Salary: ");

        scanf("%f", &employees[i].salary);

    }

   

    printf("Details of employees:\n");

    for (int i = 0; i < 3; i++) {

        printf("Employee %d - ID: %d, Name: %s, Salary: %.2f\n", i + 1, employees[i].empId, employees[i].name, employees[i].salary);

    }

   

    return 0;

}

51. Passing Structures to Functions:

#include <stdio.h>

 

struct Rectangle {

    int length;

    int width;

};

 

void displayRectangle(struct Rectangle rect) {

    printf("Length: %d\n", rect.length);

    printf("Width: %d\n", rect.width);

}

 

int main() {

    struct Rectangle myRect = {10, 5};

   

    printf("Rectangle details:\n");

    displayRectangle(myRect);

   

    return 0;

}

52. Creating and Accessing Unions:

#include <stdio.h>

 

union Data {

    int num;

    float fraction;

    char character;

};

 

int main() {

    union Data myData;

   

    myData.num = 42;

    printf("Num: %d\n", myData.num);

   

    myData.fraction = 3.14;