Lab: FizzBuzz in C
Implement the classic FizzBuzz problem in C to practice conditionals, modulo arithmetic, and for loops together.
FizzBuzz is a classic interview problem that tests whether you can combine conditionals and loops correctly. It is short enough to write in a few minutes but has just enough edge cases to reveal misunderstandings.
The problem
Print the numbers from 1 to 100. But:
- For multiples of 3, print
Fizzinstead of the number. - For multiples of 5, print
Buzzinstead of the number. - For multiples of both 3 and 5, print
FizzBuzzinstead of the number.
Expected output (first 20 lines):
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
BuzzYour task
Write fizzbuzz.c. Start from scratch — do not look at a solution yet.
Think about the order of your conditions before writing. What happens if you check n % 3 == 0 first and print "Fizz", then check n % 5 == 0 separately? Try it and see.
Common mistakes to avoid
Printing extra text for divisible-by-15 numbers. If you write:
if (n % 3 == 0) { printf("Fizz"); }
if (n % 5 == 0) { printf("Buzz"); }You get FizzBuzz as desired — but you also need to avoid printing the number on those lines. The order of the else-if chain matters.
Using else if after a print. Consider:
if (n % 15 == 0) {
printf("FizzBuzz\n");
} else if (n % 3 == 0) {
printf("Fizz\n");
} else if (n % 5 == 0) {
printf("Buzz\n");
} else {
printf("%d\n", n);
}This is the cleanest approach: check the most specific case (divisible by both) first.
A worked solution
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 100; 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;
}Extension challenges
-
Parameterise it. Change the program so it takes N (the upper bound), F (the Fizz divisor), and B (the Buzz divisor) as hardcoded constants you can change in one place, rather than magic numbers scattered through the code.
-
Alternative string building. Write a version that does not test
% 15but instead builds the output string incrementally: start with an empty string, append "Fizz" if divisible by 3, append "Buzz" if divisible by 5, then print the string — or the number if the string is still empty. This requires knowledge of C strings (covered in the Arrays and strings module). -
Count the categories. After printing, print a summary: how many Fizz, Buzz, FizzBuzz, and plain number lines appeared.
What you practised
- The
forloop with a counter variable - The modulo operator for divisibility tests
else ifchains with correctly ordered conditions- Separating the "most specific" case to avoid double-printing
The next module is Functions — how to declare reusable blocks of code, pass values in, and return results out.
break, continue, and loop patterns
Control loop execution with break and continue, and recognise common loop patterns like search, accumulation, and early exit.
Declaring and defining functions
Learn how to declare and define functions in C — including the declaration vs definition distinction, function prototypes, and void functions.