C Programming Practice Problems Solutions

C Programming Practice Problems and Solutions

1. Calculate Sum and Percentage of 5 Subjects

Write a C program (WAP) that accepts the marks of 5 subjects and finds the sum and percentage.

#include <stdio.h>
int main()
{
    int marks[5];
    int total = 0;
    float percentage;

    printf("Enter marks for 5 subjects:\n");
    for(int i = 0; i < 5; i++)
    {
        scanf("%d", &marks[i]);
        total += marks[i];
    }

    percentage = (total / 5.0);
    printf("Sum: %d\n", total);
    printf("Percentage: %.2f%%
", percentage);

    return 0;
}

2. Swap Values of Two Variables

Write a C program to swap the values of two variables.

#include <stdio.h>
int main()
{
    int x, y, a;
    printf("Enter two integers (x and y):\n");
    scanf("%d %d", &x, &y);
    printf("Before swap: x = %d, y = %d\n", x, y);

    // Swapping using a temporary variable 'a'
    a = x;
    x = y;
    y = a;

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

3. Print ASCII Value of a Character

Write a C program to print the ASCII value of a given character.

#include <stdio.h>
int main()
{
    char ch;
    printf("Enter a character:\n");
    scanf("%c", &ch);
    printf("ASCII value of '%c' is: %d\n", ch, ch);
    return 0;
}

4. Check for Leap Year

Write a C program to check if a given year is a leap year.

#include <stdio.h>
int main()
{
    int year;
    printf("Enter a year:\n");
    scanf("%d", &year);

    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    {
        printf("%d is a Leap year\n", year);
    }
    else
    {
        printf("%d is Not a leap year\n", year);
    }

    return 0;
}

5. Simple Calculator Using Switch

Write a C program to design a simple calculator using a switch statement.

#include <stdio.h>
int main() {
    char o;
    float num1, num2, result;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &o);

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

    switch(o)
    {
        case '+':
            result = num1 + num2;
            printf("Result: %.2f\n", result);
            break;

        case '-':
            result = num1 - num2;
            printf("Result: %.2f\n", result);
            break;

        case '*':
            result = num1 * num2;
            printf("Result: %.2f\n", result);
            break;

        case '/':
            if (num2 == 0)
            {
                printf("Error: Division by zero is not allowed\n");
            }
            else
            {
                result = num1 / num2;
                printf("Result: %.2f\n", result);
            }
            break;

        default:
            printf("Error: Invalid operator\n");
    }

    return 0;
}

6. Check Prime Number

Write a C program to check if a number is prime.

#include <stdio.h>
void main() {
    int a, b, is_prime = 1;
    printf("Enter an integer:\n");
    scanf("%d", &a);

    if (a <= 1)
    {
        printf("Not Prime\n");
    }
    else
    {
        for (b = 2; b * b <= a; b++)
        {
            if (a % b == 0)
            {
                is_prime = 0;
                break;
            }
        }

        if (is_prime)
            printf("Prime\n");
        else
            printf("Not Prime\n");
    }
}

7. Print Fibonacci Series

Write a C program to print the Fibonacci series up to n terms.

#include <stdio.h>
void main()
{
    int a, b;
    long long first = 0, second = 1, next;

    printf("Enter the number of terms:\n");
    scanf("%d", &a);

    for (b = 1; b <= a; b++)
    {
        printf("%lld ", first);

        if (b < a)
            next = first + second;

        first = second;
        second = next;
    }
    printf("\n");
}

8. Print Sum Up to a Number

Write a C program to calculate the sum of natural numbers up to a given number n.

#include <stdio.h>
int main()
{
    int n;
    printf("Enter a positive integer:\n");
    scanf("%d", &n);

    if (n <= 0)
    {
        printf("Invalid input\n");
    }
    else
    {
        // Using the formula: Sum = n * (n + 1) / 2
        int sum = n * (n + 1) / 2;
        printf("Sum up to %d is: %d\n", n, sum);
    }

    return 0;
}

9. Sort an Array Using Bubble Sort

Write a C program to sort an array using the Bubble Sort algorithm.

#include <stdio.h>

void main()
{
    int n;
    printf("Enter the size of the array:\n");
    scanf("%d", &n);

    if (n <= 0) return;

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

    // Bubble Sort implementation
    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;
            }
        }
    }

    printf("Sorted array:\n");
    for (int i = 0; i < n; i++)
    {
        printf("%d", arr[i]);
        if (i != n - 1)
        {
            printf(" ");
        }
    }
    printf("\n");
}

10. Check Palindrome Using a Function

Write a C program to check if a number is a palindrome using a separate function.

#include <stdio.h>

int isPalindrome(int n)
{
    int remainder, reversed_sum = 0, original_copy = n;

    while (n > 0)
    {
        remainder = n % 10;
        reversed_sum = reversed_sum * 10 + remainder;
        n = n / 10;
    }

    if (original_copy != reversed_sum)
        return 0; // False
    else
        return 1; // True
}

int main()
{
    int num;
    printf("Enter an integer:\n");
    scanf("%d", &num);

    if (isPalindrome(num))
    {
        printf("%d is a palindrome\n", num);
    }
    else
    {
        printf("%d is not a palindrome\n", num);
    }

    return 0;
}

11. Print Array Using a Function

Write a C program to print the elements of an array using a function.

#include <stdio.h>

void printArray(int a[], int n)
{
    printf("Array elements: ");
    for (int i = 0; i < n; i++)
        printf("%d ", a[i]);
    printf("\n");
}

int main()
{
    int a[50], n, i;
    printf("Enter the size of the array (max 50):\n");
    scanf("%d", &n);

    if (n > 50 || n <= 0) return 1;

    printf("Enter %d elements:\n", n);
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    printArray(a, n);
    return 0;
}

12. Reverse a String Without Library Functions

Write a C program to reverse a string without using standard library functions like strlen.

#include <stdio.h>

void main()
{
    char str[50], temp_char;
    int i, length = 0;

    printf("Enter a string:\n");
    // Read string including spaces
    scanf("%[^
]%*c", str);

    // Calculate length manually
    while (str[length] != '\0')
    {
        length++;
    }

    // Reverse the string in place
    int start = 0;
    int end = length - 1;

    while (start < end)
    {
        // Swap characters
        temp_char = str[start];
        str[start] = str[end];
        str[end] = temp_char;

        start++;
        end--;
    }

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

13. Find Frequency of a Character

Write a C program to find the frequency (count) of a specific character in a string.

#include <stdio.h>
// Note: string.h is not strictly needed if we iterate until '\0'

int main()
{
    char str[1000];
    char ch;
    int count = 0;

    printf("Enter a string:\n");
    scanf("%s", str);
    printf("Enter the character to count:\n");
    scanf(" %c", &ch); // Note the space before %c to consume leftover newline

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

    printf("Frequency of '%c': %d\n", ch, count);
    return 0;
}

14. Check Armstrong Number

Write a C program to check if a number is an Armstrong number.

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

int main()
{
    int n, originalNumber, remainder, numDigits = 0;
    long long result = 0;

    printf("Enter an integer:\n");
    scanf("%d", &n);

    originalNumber = n;

    // Calculate the number of digits
    if (originalNumber == 0)
    {
        numDigits = 1;
    }
    else
    {
        int temp = originalNumber;
        while (temp != 0)
        {
            temp /= 10;
            ++numDigits;
        }
    }

    // Calculate the sum of powers of digits
    int temp = originalNumber;
    while (temp != 0)
    {
        remainder = temp % 10;
        result += (long long)pow(remainder, numDigits);
        temp /= 10;
    }

    if (result == originalNumber)
    {
        printf("%d is an Armstrong number\n", originalNumber);
    }
    else
    {
        printf("%d is not an Armstrong number\n", originalNumber);
    }

    return 0;
}