Intermediate Level (Level 2)

Focus: Functions, arrays, string operations, user-defined logic.

1. Write a function to calculate the factorial of a number using recursion.

cCopyEdit#include <stdio.h>

int factorial(int n) {
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}

int main() {
    int num = 5;
    printf("Factorial of %d = %d\n", num, factorial(num));
    return 0;
}

Explanation:
This uses recursion: a function calling itself. Base case is factorial(0) = 1.


2. Write a program to reverse the elements of an array.

cCopyEdit#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5}, n = 5;
    for (int i = 0; i < n / 2; i++) {
        int temp = arr[i];
        arr[i] = arr[n - 1 - i];
        arr[n - 1 - i] = temp;
    }

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

Explanation:
We swap the first and last elements, then move inward to reverse the array.


3. Write a function to check whether a number is prime.

cCopyEdit#include <stdio.h>

int isPrime(int n) {
    if (n <= 1) return 0;
    for (int i = 2; i <= n / 2; i++)
        if (n % i == 0) return 0;
    return 1;
}

int main() {
    int num = 17;
    if (isPrime(num))
        printf("%d is Prime\n", num);
    else
        printf("%d is not Prime\n", num);
    return 0;
}

Explanation:
Check divisibility up to n/2. A prime has no divisors other than 1 and itself.


4. Write a program to find the largest element in an array.

cCopyEdit#include <stdio.h>

int main() {
    int arr[] = {23, 45, 12, 67, 34}, n = 5, max = arr[0];
    for (int i = 1; i < n; i++)
        if (arr[i] > max)
            max = arr[i];

    printf("Largest element: %d\n", max);
    return 0;
}

Explanation:
Iterate and keep updating max if a larger number is found.


5. Write a program to count vowels in a string.

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

int main() {
    char str[] = "Hello World";
    int count = 0;

    for (int i = 0; str[i] != '\0'; i++) {
        char ch = str[i] | 32;  // Convert to lowercase
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
            count++;
    }

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

Explanation:
Using bitwise OR | 32 converts any uppercase letter to lowercase efficiently.


6. Write a function to calculate the sum of all elements in an array.

cCopyEdit#include <stdio.h>

int arraySum(int arr[], int n) {
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
    return sum;
}

int main() {
    int arr[] = {5, 10, 15, 20}, n = 4;
    printf("Sum = %d\n", arraySum(arr, n));
    return 0;
}

Explanation:
A simple for loop accumulates all values into sum.


7. Write a program to check if a string is a palindrome.

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

int main() {
    char str[] = "madam";
    int len = strlen(str), flag = 1;

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

    if (flag)
        printf("Palindrome\n");
    else
        printf("Not Palindrome\n");

    return 0;
}

Explanation:
Compare characters from start and end. If all match, it’s a palindrome.


8. Write a function to calculate the average of numbers in an array.

cCopyEdit#include <stdio.h>

float average(int arr[], int n) {
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
    return (float)sum / n;
}

int main() {
    int arr[] = {10, 20, 30, 40}, n = 4;
    printf("Average = %.2f\n", average(arr, n));
    return 0;
}

Explanation:
Sum the numbers and divide by total elements. Cast to float for decimal output.


9. Write a program to merge two arrays into a third array.

cCopyEdit#include <stdio.h>

int main() {
    int a[] = {1, 2, 3}, b[] = {4, 5, 6}, c[6];
    for (int i = 0; i < 3; i++)
        c[i] = a[i];
    for (int i = 0; i < 3; i++)
        c[i + 3] = b[i];

    printf("Merged array: ");
    for (int i = 0; i < 6; i++)
        printf("%d ", c[i]);
    return 0;
}

Explanation:
Copy each array into different parts of the third array.


10. Write a program to count the frequency of each character in a string.

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

int main() {
    char str[] = "programming";
    int freq[256] = {0};  // ASCII limit

    for (int i = 0; str[i] != '\0'; i++)
        freq[(unsigned char)str[i]]++;

    printf("Character frequencies:\n");
    for (int i = 0; i < 256; i++)
        if (freq[i] > 0)
            printf("%c: %d\n", i, freq[i]);

    return 0;
}

Explanation:
Use an array to count how many times each ASCII character appears.

Scroll to Top