C exercises with solutions
1. Sum from 1 to 100 Beginner
Use a loop to compute the sum of the numbers from 1 to 100 and print it.
Expected output: 5050
Note
Start a variable summe at 0 and add i in a for loop. Print with printf("%d\n", summe);.
Show sample solution
#include <stdio.h>
int main(void) {
int summe = 0;
for (int i = 1; i <= 100; i++) {
summe += i;
}
printf("%d\n", summe);
return 0;
}2. FizzBuzz (1–15) Beginner
Print the numbers 1 to 15, each on its own line. For multiples of 3 print Fizz instead, for multiples of 5 print Buzz, and for both print FizzBuzz.
Expected output: 1 ⏎ 2 ⏎ Fizz ⏎ 4 ⏎ Buzz ⏎ Fizz ⏎ 7 ⏎ 8 ⏎ Fizz ⏎ Buzz ⏎ 11 ⏎ Fizz ⏎ 13 ⏎ 14 ⏎ FizzBuzz
Note
Check the case "divisible by 15" first, then 3, then 5. You test divisibility with i % 3 == 0.
Show sample solution
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 15; i++) {
if (i % 15 == 0) printf("FizzBuzz\n");
else if (i % 3 == 0) printf("Fizz\n");
else if (i % 5 == 0) printf("Buzz\n");
else printf("%d\n", i);
}
return 0;
}3. Factorial as a function Beginner
Write a function unsigned long long fakultaet(unsigned n) and use it to print 5! and 10! (each on its own line).
Expected output: 120 ⏎ 3628800
Note
Iteratively: ergebnis = 1, then multiply in a loop from 2 to n. Alternatively recursively: n <= 1 ? 1 : n * fakultaet(n - 1).
Show sample solution
#include <stdio.h>
unsigned long long fakultaet(unsigned n) {
unsigned long long ergebnis = 1;
for (unsigned i = 2; i <= n; i++) {
ergebnis *= i;
}
return ergebnis;
}
int main(void) {
printf("%llu\n", fakultaet(5));
printf("%llu\n", fakultaet(10));
return 0;
}4. Maximum and average Beginner
Determine the maximum and the average of the array {12, 7, 25, 3, 18}. First print the maximum, then the average with one decimal place (each on one line).
Expected output: 25 ⏎ 13.0
Note
Sum in a long variable and divide with a cast: (double)summe / n. The maximum starts at z[0].
Show sample solution
#include <stdio.h>
int main(void) {
int z[] = {12, 7, 25, 3, 18};
size_t n = sizeof z / sizeof z[0];
int max = z[0];
long summe = 0;
for (size_t i = 0; i < n; i++) {
if (z[i] > max) max = z[i];
summe += z[i];
}
printf("%d\n", max);
printf("%.1f\n", (double)summe / n);
return 0;
}5. Reverse a string in place Intermediate
Write a function void umkehren(char *s) that reverses a string in place. Reverse "Hello World" and print the result.
Expected output: dlroW olleH
Note
Two indices: i = 0 and j = strlen(s) - 1 (careful with an empty string). Swap s[i] and s[j] until the indices meet.
Show sample solution
#include <stdio.h>
#include <string.h>
void umkehren(char *s) {
size_t n = strlen(s);
for (size_t i = 0; i < n / 2; i++) {
char t = s[i];
s[i] = s[n - 1 - i];
s[n - 1 - i] = t;
}
}
int main(void) {
char text[] = "Hello World";
umkehren(text);
printf("%s\n", text);
return 0;
}6. Minimum and maximum via pointers Intermediate
Write void min_max(const int *a, size_t n, int *min, int *max), which delivers the minimum and maximum through output parameters. Test with {4, -2, 9, 0, 7} and print min=-2 max=9.
Expected output: min=-2 max=9
Note
Initialize *min and *max with a[0] and update them in a loop starting at index 1. Access the results via *min and *max.
Show sample solution
#include <stdio.h>
void min_max(const int *a, size_t n, int *min, int *max) {
*min = *max = a[0];
for (size_t i = 1; i < n; i++) {
if (a[i] < *min) *min = a[i];
if (a[i] > *max) *max = a[i];
}
}
int main(void) {
int z[] = {4, -2, 9, 0, 7};
int lo, hi;
min_max(z, 5, &lo, &hi);
printf("min=%d max=%d\n", lo, hi);
return 0;
}7. Square numbers on the heap Intermediate
Use malloc to create an int array for 6 elements, store the squares of 0 to 5 in it, print them separated by spaces (with a trailing newline) and free the memory.
Expected output: 0 1 4 9 16 25
Note
int *a = malloc(n * sizeof *a); – then be sure to check for NULL. At the end, free(a);.
Show sample solution
#include <stdio.h>
#include <stdlib.h>
int main(void) {
size_t n = 6;
int *a = malloc(n * sizeof *a);
if (a == NULL) return 1;
for (size_t i = 0; i < n; i++) a[i] = (int)(i * i);
for (size_t i = 0; i < n; i++) {
printf("%d%s", a[i], i + 1 < n ? " " : "\n");
}
free(a);
return 0;
}8. Count vowels Intermediate
Count the vowels (a, e, i, o, u – case does not matter) in "Programming in C is fun" and print the count.
Expected output: 6
Note
Walk over the string with for (const char *p = text; *p; p++), convert with tolower((unsigned char)*p) and check with strchr("aeiou", c) or a switch. Note: strchr also finds the null byte – check c != 0.
Show sample solution
#include <stdio.h>
#include <ctype.h>
int main(void) {
const char *text = "Programming in C is fun";
int anzahl = 0;
for (const char *p = text; *p != '\0'; p++) {
switch (tolower((unsigned char)*p)) {
case 'a': case 'e': case 'i': case 'o': case 'u':
anzahl++;
}
}
printf("%d\n", anzahl);
return 0;
}9. Reverse a linked list Advanced
Build a list 1 -> 2 -> 3, reverse it by redirecting the next pointers (without allocating new nodes), print the values (3 2 1) and free the list.
Expected output: 3 2 1
Note
Three pointers: vorher (initially NULL), aktuell (the head) and naechster. In each step: naechster = aktuell->next; aktuell->next = vorher; vorher = aktuell; aktuell = naechster;. Return: vorher.
Show sample solution
#include <stdio.h>
#include <stdlib.h>
typedef struct Node { int wert; struct Node *next; } Node;
Node *neu(int wert, Node *next) {
Node *n = malloc(sizeof *n);
if (n == NULL) exit(EXIT_FAILURE);
n->wert = wert;
n->next = next;
return n;
}
Node *umkehren(Node *kopf) {
Node *vorher = NULL;
Node *aktuell = kopf;
while (aktuell != NULL) {
Node *naechster = aktuell->next;
aktuell->next = vorher;
vorher = aktuell;
aktuell = naechster;
}
return vorher;
}
int main(void) {
Node *liste = neu(1, neu(2, neu(3, NULL)));
liste = umkehren(liste);
for (Node *n = liste; n != NULL; n = n->next) printf("%d ", n->wert);
printf("\n");
while (liste != NULL) {
Node *w = liste->next;
free(liste);
liste = w;
}
return 0;
}10. Set bits and powers of two Advanced
Write int popcount(unsigned x) (number of set bits) and int ist_zweierpotenz(unsigned x) (1 if x is a power of two, otherwise 0; 0 is not one). Print popcount(255), popcount(1024), ist_zweierpotenz(64) and ist_zweierpotenz(96), each on one line.
Expected output: 8 ⏎ 1 ⏎ 1 ⏎ 0
Note
x & (x - 1) clears the lowest set bit – count how many times you can do that until x is 0. A power of two has exactly one set bit: x != 0 && (x & (x - 1)) == 0.
Show sample solution
#include <stdio.h>
int popcount(unsigned x) {
int n = 0;
while (x != 0) {
x &= x - 1;
n++;
}
return n;
}
int ist_zweierpotenz(unsigned x) {
return x != 0 && (x & (x - 1)) == 0;
}
int main(void) {
printf("%d\n", popcount(255));
printf("%d\n", popcount(1024));
printf("%d\n", ist_zweierpotenz(64));
printf("%d\n", ist_zweierpotenz(96));
return 0;
}