Blog Posts > C Programming Practice: 50 Exercises With Solutions

C Programming Practice: 50 Exercises With Solut...

This C programming practice guide gives you 50 exercises across six categories: basics, loops, arrays, strings, pointers, and functions. Each section explains what to practice and shows worked examples with complete, compile-ready solutions. Type every program yourself, compile it, and change one thing to see what happens.

How to use this C programming practice set

Install a compiler first. On most Bangladeshi setups, GCC works well. Save your file as name.c, then compile and run it from the terminal:

gcc name.c -o name
./name

Do not copy-paste blindly. Read the exercise, attempt it on paper or in your editor, then compare with the solution. If your code fails, read the compiler error line by line. Fixing your own bugs teaches more than any tutorial. Aim for two or three exercises per sitting, not all fifty at once.

Category 1: Basics (Exercises 1-8)

These cover input/output, variables, arithmetic, and conditionals. Practice these: print your name, add two numbers, find area of a circle, convert Celsius to Fahrenheit, check even or odd, find the larger of two numbers, swap two variables, and check if a year is a leap year.

Worked example: check even or odd

#include <stdio.h>

int main(void) {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (n % 2 == 0)
        printf("%d is even\n", n);
    else
        printf("%d is odd\n", n);
    return 0;
}

The % operator gives the remainder. If a number divided by 2 leaves no remainder, it is even.

Category 2: Loops (Exercises 9-18)

Loops repeat work. Practice these: print 1 to 100, sum of first N numbers, multiplication table, factorial, count digits in a number, reverse a number, check for a prime number, print a star triangle, sum of digits, and the Fibonacci series.

Worked example: factorial of a number

#include <stdio.h>

int main(void) {
    int n;
    long long fact = 1;
    printf("Enter a number: ");
    scanf("%d", &n);
    for (int i = 2; i <= n; i++)
        fact *= i;
    printf("Factorial of %d = %lld\n", n, fact);
    return 0;
}

We start fact at 1 and multiply by every integer up to n. Using long long holds larger results than int.

Category 3: Arrays (Exercises 19-28)

Arrays store many values of the same type. Practice these: read N numbers and print them, find the largest element, find the smallest, sum and average, count even and odd elements, reverse an array, search for a value, sort with bubble sort, merge two arrays, and remove duplicates.

Worked example: find the largest element

#include <stdio.h>

int main(void) {
    int n;
    printf("How many numbers? ");
    scanf("%d", &n);
    int a[100];
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    int max = a[0];
    for (int i = 1; i < n; i++)
        if (a[i] > max)
            max = a[i];

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

Assume the first element is the largest, then compare it against every other element and update when you find something bigger.

Category 4: Strings (Exercises 29-37)

In C, a string is a character array ending with '\0'. Practice these: read a name and greet the user, find string length, count vowels, convert to uppercase, reverse a string, check for a palindrome, count words, copy one string to another, and compare two strings.

Worked example: count vowels in a string

#include <stdio.h>

int main(void) {
    char s[100];
    printf("Enter text: ");
    fgets(s, sizeof(s), stdin);

    int count = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        char c = s[i];
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
            c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
            count++;
    }
    printf("Vowels: %d\n", count);
    return 0;
}

We use fgets instead of scanf so spaces are read too. The loop stops at the null terminator '\0'.

Category 5: Functions (Exercises 38-44)

Functions break a program into reusable blocks. Practice these: a function that adds two numbers, one that returns the maximum, a factorial function, a function to check prime, a recursive sum, a power function, and a function that returns whether a number is prime as a yes/no value.

Worked example: a reusable max function

#include <stdio.h>

int max(int a, int b) {
    return (a > b) ? a : b;
}

int main(void) {
    int x, y;
    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);
    printf("Max = %d\n", max(x, y));
    return 0;
}

The function takes two parameters and returns one value. The ternary operator ?: is a short way to write a simple if-else.

Category 6: Pointers (Exercises 45-50)

Pointers store memory addresses. They are the part beginners fear most, so go slowly. Practice these: print the address of a variable, change a value through a pointer, swap two numbers using pointers, walk an array with a pointer, pass an array to a function, and find string length using pointer movement.

Worked example: swap two numbers using pointers

#include <stdio.h>

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

int main(void) {
    int x = 5, y = 9;
    swap(&x, &y);
    printf("x = %d, y = %d\n", x, y);
    return 0;
}

We pass the addresses of x and y. Inside swap, *a means "the value stored at that address," so changes affect the original variables.

A simple practice routine that works

Pick one category per day. Solve two exercises from memory, then check the solution. On day seven, redo the hardest problem from each category without looking. Keep a single folder of your .c files so you can see your progress. When a program behaves strangely, add printf lines to print variable values and trace what is happening.

Consistent daily practice in C builds a strong base for C++, embedded systems, and competitive programming. Once you can solve these comfortably, a software role becomes realistic; you can explore openings at Avian™ Career to see what local employers expect.

Frequently asked questions

Which compiler should a beginner in Bangladesh use?
GCC is the standard free choice and runs on Windows, Linux, and macOS. Many students install it through Code::Blocks or MinGW on Windows. Online compilers also work when you do not have a machine handy. The exercises here compile cleanly with any standard C compiler.

How long does it take to finish 50 C exercises?
If you solve two or three each day and actually debug your own mistakes, two to three weeks is realistic for a beginner. Speed matters less than understanding. It is far better to deeply grasp ten programs than to rush through fifty by copying answers.

Why does my C program crash with no clear error?
Crashes usually come from reading or writing memory you do not own, often through a bad array index or an uninitialized pointer. Check that array indexes stay inside bounds and that every pointer points somewhere valid before you use * on it.

Should I learn pointers before arrays and strings?
No. Learn basics, loops, and arrays first, then strings, and tackle pointers last. Pointers make far more sense once you already understand how arrays sit in memory. Following this order, the way that pointers and arrays connect will feel natural rather than confusing.


Categories

  • Tutorial
  • Announcement
  • News