Code of the Day
BeginnerControl flow

Lab: FizzBuzz in C

Implement the classic FizzBuzz problem in C to practice conditionals, modulo arithmetic, and for loops together.

Lab · optionalCBeginner20 min

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 Fizz instead of the number.
  • For multiples of 5, print Buzz instead of the number.
  • For multiples of both 3 and 5, print FizzBuzz instead 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
Buzz

Your 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

  1. 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.

  2. Alternative string building. Write a version that does not test % 15 but 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).

  3. Count the categories. After printing, print a summary: how many Fizz, Buzz, FizzBuzz, and plain number lines appeared.

What you practised

  • The for loop with a counter variable
  • The modulo operator for divisibility tests
  • else if chains 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.

Finished reading? Mark it complete to track your progress.

On this page