C course
c programming language icon

C Exercises: 24 Practice Problems with Solutions

C makes memory visible, which is why it is still the best language for understanding what a program really does. These problems move from printf and loops to arrays, strings, pointers, dynamic allocation and structs.

Each solution is a full program including its headers. Compile and run it in the editor below; problems that call scanf need values in the Input box.

Run your answer here

Type your solution, press Run, and use the Input box when a problem asks you to read from standard input.

C Compiler
Output
Code runs on the Play with Coding execution engine — your code is saved locally for next time.

How to practise so it actually sticks

Attempt the problem before opening the solution, even if your first version is clumsy. A working ugly answer teaches more than a beautiful one you read. When you get stuck for more than five minutes, read the hint — not the solution — and try again.

After you pass, open the solution and ask what is different about it. Shorter? Fewer variables? A built-in you did not know? That comparison is where most of the growth happens. Then change the problem slightly: sort the other direction, handle an empty input, read a value instead of hard-coding it.

Aim for three to five problems a day rather than thirty in one sitting. Spacing practice over days is what moves syntax from "I can look it up" to "my fingers know it".

The exercises

Showing 24 of 24 exercises.

  1. BeginnerBasics & output

    1. Print a greeting

    Print "Hello, Ada!" using a char array for the name.

    Hint: printf with %s.

    #include <stdio.h>
    
    int main(void) {
        char name[] = "Ada";
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        char name[] = "Ada";
        printf("Hello, %s!\n", name);
        return 0;
    }
  2. BeginnerBasics & output

    2. Sizes of the basic types

    Print sizeof(char), sizeof(int), sizeof(float) and sizeof(double).

    Hint: sizeof yields size_t — print with %zu.

    #include <stdio.h>
    
    int main(void) {
        
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        printf("%zu %zu %zu %zu\n", sizeof(char), sizeof(int), sizeof(float), sizeof(double));
        return 0;
    }
  3. BeginnerBasics & output

    3. Read two numbers

    Read two integers from input and print their sum. Put two numbers in the Input box.

    Hint: scanf("%d %d", &a, &b) — remember the ampersands.

    #include <stdio.h>
    
    int main(void) {
        int a, b;
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        int a, b;
        scanf("%d %d", &a, &b);
        printf("%d\n", a + b);
        return 0;
    }
  4. BeginnerConditionals

    4. Even or odd

    Print "even" or "odd" for 17.

    Hint: n % 2 with an if/else.

    #include <stdio.h>
    
    int main(void) {
        int n = 17;
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        int n = 17;
        printf("%s\n", n % 2 == 0 ? "even" : "odd");
        return 0;
    }
  5. BeginnerConditionals

    5. Largest of three

    Print the largest of 12, 45 and 31 using if statements only.

    Hint: Start with the first value as the maximum.

    #include <stdio.h>
    
    int main(void) {
        int a = 12, b = 45, c = 31;
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        int a = 12, b = 45, c = 31;
        int max = a;
        if (b > max) max = b;
        if (c > max) max = c;
        printf("%d\n", max);
        return 0;
    }
  6. BeginnerLoops

    6. FizzBuzz to 20

    Print 1..20, replacing multiples of 3 with "Fizz", 5 with "Buzz", both with "FizzBuzz".

    Hint: Check the 15 case first.

    #include <stdio.h>
    
    int main(void) {
        for (int i = 1; i <= 20; i++) {
        }
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        for (int i = 1; i <= 20; i++) {
            if (i % 15 == 0) puts("FizzBuzz");
            else if (i % 3 == 0) puts("Fizz");
            else if (i % 5 == 0) puts("Buzz");
            else printf("%d\n", i);
        }
        return 0;
    }
  7. BeginnerLoops

    7. Factorial with a loop

    Compute 10! and print it.

    Hint: Use long long to be safe.

    #include <stdio.h>
    
    int main(void) {
        long long r = 1;
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        long long r = 1;
        for (int i = 2; i <= 10; i++) r *= i;
        printf("%lld\n", r);
        return 0;
    }
  8. BeginnerLoops

    8. Star triangle

    Print a right-angled triangle of five rows of stars.

    Hint: Nested loops and putchar.

    #include <stdio.h>
    
    int main(void) {
        
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 0; j < i; j++) putchar('*');
            putchar('\n');
        }
        return 0;
    }
  9. BeginnerArrays

    9. Array statistics

    For {4, 9, 1, 7, 3} print the sum, the maximum and the minimum.

    Hint: Compute the length as sizeof(a) / sizeof(a[0]).

    #include <stdio.h>
    
    int main(void) {
        int a[] = {4, 9, 1, 7, 3};
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        int a[] = {4, 9, 1, 7, 3};
        int n = sizeof(a) / sizeof(a[0]);
        int sum = 0, max = a[0], min = a[0];
        for (int i = 0; i < n; i++) {
            sum += a[i];
            if (a[i] > max) max = a[i];
            if (a[i] < min) min = a[i];
        }
        printf("%d %d %d\n", sum, max, min);
        return 0;
    }
  10. IntermediateArrays

    10. Reverse an array in place

    Reverse {1,2,3,4,5} in place and print it.

    Hint: Swap from both ends towards the middle.

    #include <stdio.h>
    
    int main(void) {
        int a[] = {1, 2, 3, 4, 5};
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        int a[] = {1, 2, 3, 4, 5};
        int n = 5;
        for (int i = 0, j = n - 1; i < j; i++, j--) {
            int t = a[i]; a[i] = a[j]; a[j] = t;
        }
        for (int i = 0; i < n; i++) printf("%d ", a[i]);
        putchar('\n');
        return 0;
    }
  11. IntermediateArrays

    11. Sum a 2D matrix

    Sum every element of a 3x3 matrix and print each row total too.

    Hint: Nested loops over rows and columns.

    #include <stdio.h>
    
    int main(void) {
        int m[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        int m[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
        int total = 0;
        for (int i = 0; i < 3; i++) {
            int row = 0;
            for (int j = 0; j < 3; j++) row += m[i][j];
            printf("row %d = %d\n", i, row);
            total += row;
        }
        printf("total = %d\n", total);
        return 0;
    }
  12. BeginnerStrings

    12. String length by hand

    Count the characters in "programming" without using strlen.

    Hint: Walk until you reach the terminating '\0'.

    #include <stdio.h>
    
    int main(void) {
        char s[] = "programming";
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        char s[] = "programming";
        int n = 0;
        while (s[n] != '\0') n++;
        printf("%d\n", n);
        return 0;
    }
  13. IntermediateStrings

    13. Reverse a string

    Reverse "language" in place and print it.

    Hint: Use strlen from <string.h> then swap ends.

    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char s[] = "language";
        return 0;
    }
    Show solution
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char s[] = "language";
        int n = strlen(s);
        for (int i = 0, j = n - 1; i < j; i++, j--) {
            char t = s[i]; s[i] = s[j]; s[j] = t;
        }
        puts(s);
        return 0;
    }
  14. IntermediateStrings

    14. Count vowels

    Count the vowels in "Programming in C is fun".

    Hint: strchr("aeiouAEIOU", ch) is a neat test.

    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char s[] = "Programming in C is fun";
        return 0;
    }
    Show solution
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char s[] = "Programming in C is fun";
        int n = 0;
        for (int i = 0; s[i]; i++)
            if (strchr("aeiouAEIOU", s[i])) n++;
        printf("%d\n", n);
        return 0;
    }
  15. BeginnerFunctions

    15. Write a function

    Write int add(int, int) above main and call it.

    Hint: Define it before main or declare a prototype.

    #include <stdio.h>
    
    int main(void) {
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int add(int a, int b) {
        return a + b;
    }
    
    int main(void) {
        printf("%d\n", add(2, 3));
        return 0;
    }
  16. IntermediatePointers

    16. Swap with pointers

    Write swap(int *a, int *b) and swap two values in main.

    Hint: Dereference with *a to read and write the caller's variables.

    #include <stdio.h>
    
    int main(void) {
        int x = 3, y = 8;
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    void swap(int *a, int *b) {
        int t = *a; *a = *b; *b = t;
    }
    
    int main(void) {
        int x = 3, y = 8;
        swap(&x, &y);
        printf("%d %d\n", x, y);
        return 0;
    }
  17. IntermediatePointers

    17. Walk an array with a pointer

    Sum an int array using pointer arithmetic instead of indexing.

    Hint: for (int *p = a; p < a + n; p++).

    #include <stdio.h>
    
    int main(void) {
        int a[] = {2, 4, 6, 8};
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        int a[] = {2, 4, 6, 8};
        int n = 4, sum = 0;
        for (int *p = a; p < a + n; p++) sum += *p;
        printf("%d\n", sum);
        return 0;
    }
  18. AdvancedPointers

    18. Allocate memory dynamically

    Allocate an array of 5 ints with malloc, fill it with squares, print and free it.

    Hint: Always check the malloc result and free at the end.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        
        return 0;
    }
    Show solution
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        int n = 5;
        int *a = malloc(n * sizeof(int));
        if (!a) return 1;
        for (int i = 0; i < n; i++) a[i] = i * i;
        for (int i = 0; i < n; i++) printf("%d ", a[i]);
        putchar('\n');
        free(a);
        return 0;
    }
  19. IntermediateStructs

    19. Define a struct

    Define struct Point with x and y, create two points and print the midpoint.

    Hint: Access fields with the dot operator.

    #include <stdio.h>
    
    int main(void) {
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    struct Point { double x, y; };
    
    int main(void) {
        struct Point a = {0, 0}, b = {4, 6};
        printf("%.1f %.1f\n", (a.x + b.x) / 2, (a.y + b.y) / 2);
        return 0;
    }
  20. AdvancedStructs

    20. Array of structs

    Store three students with a name and score, then print the highest scorer.

    Hint: Track the index of the best score while looping.

    #include <stdio.h>
    
    int main(void) {
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    struct Student { char name[20]; int score; };
    
    int main(void) {
        struct Student s[3] = {{"Ada", 91}, {"Ben", 74}, {"Cy", 88}};
        int best = 0;
        for (int i = 1; i < 3; i++) if (s[i].score > s[best].score) best = i;
        printf("%s %d\n", s[best].name, s[best].score);
        return 0;
    }
  21. IntermediateAlgorithms

    21. Recursive Fibonacci

    Write a recursive fib(n) and print the first ten values.

    Hint: fib(0) = 0, fib(1) = 1.

    #include <stdio.h>
    
    int main(void) {
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int fib(int n) {
        return n < 2 ? n : fib(n - 1) + fib(n - 2);
    }
    
    int main(void) {
        for (int i = 0; i < 10; i++) printf("%d ", fib(i));
        putchar('\n');
        return 0;
    }
  22. AdvancedAlgorithms

    22. Bubble sort

    Sort {5, 1, 4, 2, 8} with bubble sort and print the result.

    Hint: Swap neighbours when out of order.

    #include <stdio.h>
    
    int main(void) {
        int a[] = {5, 1, 4, 2, 8};
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        int a[] = {5, 1, 4, 2, 8};
        int n = 5;
        for (int i = 0; i < n - 1; i++)
            for (int j = 0; j < n - 1 - i; j++)
                if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; }
        for (int i = 0; i < n; i++) printf("%d ", a[i]);
        putchar('\n');
        return 0;
    }
  23. AdvancedAlgorithms

    23. Primes below 50

    Print every prime number below 50.

    Hint: Test divisors while d * d <= n.

    #include <stdio.h>
    
    int main(void) {
        
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        for (int n = 2; n < 50; n++) {
            int prime = 1;
            for (int d = 2; d * d <= n; d++) if (n % d == 0) { prime = 0; break; }
            if (prime) printf("%d ", n);
        }
        putchar('\n');
        return 0;
    }
  24. AdvancedAlgorithms

    24. Binary search

    Find 9 in the sorted array {1,3,5,7,9,11} and print its index or -1.

    Hint: Keep lo and hi bounds and compare the middle element.

    #include <stdio.h>
    
    int main(void) {
        int a[] = {1, 3, 5, 7, 9, 11};
        return 0;
    }
    Show solution
    #include <stdio.h>
    
    int main(void) {
        int a[] = {1, 3, 5, 7, 9, 11};
        int lo = 0, hi = 5, target = 9, found = -1;
        while (lo <= hi) {
            int mid = (lo + hi) / 2;
            if (a[mid] == target) { found = mid; break; }
            if (a[mid] < target) lo = mid + 1; else hi = mid - 1;
        }
        printf("%d\n", found);
        return 0;
    }

What to do next

If a whole topic feels shaky, go back to that chapter in the 17-chapter C course and re-read it, then return here. When the advanced problems feel routine, take the final C quiz and claim your certificate, or open the C online compiler and build something of your own.