basics · beginner · ~15 min

FizzBuzz to N

Combine a for-loop with chained conditionals — and learn that case ORDER matters.

Challenge

The classic loop + conditional warm-up

FizzBuzz is the interview-screening exercise that filters out people who can't write a simple loop. It's deliberately not hard — but it teaches you to be precise about exactly which condition fires first.

Task

Write a program that prints the numbers 1 through 100, one per line, with these substitutions:

  • If the number is divisible by both 3 and 5, print FizzBuzz.
  • Else if divisible by 3 only, print Fizz.
  • Else if divisible by 5 only, print Buzz.
  • Otherwise, print the number itself.

Function signature

int main(void);

Output

100 lines. Each is one of: FizzBuzz, Fizz, Buzz, or a number 1–100.

Rules

  • Check the divisible-by-15 case first — otherwise you'll print Fizz for 15 (wrong).
  • Use a for loop from 1 to 100 inclusive.
  • One printf per iteration.

Examples

n output
1 1
3 Fizz
5 Buzz
15 FizzBuzz
30 FizzBuzz
100 Buzz

Edge cases

  • Off-by-one: the loop is inclusive of 100.
  • Newline at the end of every line (including the last).

Hints

  1. Conceptual: the order of your if checks matters. if (n % 3 == 0) is true for 15, so check the AND case first.
  2. Implementation: for (int i = 1; i <= 100; i++) { … }. Inside, check (i % 15 == 0) first, then % 3, then % 5, else print the number.
  3. Common bug: writing if (i % 3 == 0 && i % 5 == 0) separately AND then if (i % 3 == 0) without else. Both fire for 15 and you double-print.

Common mistakes

  • Forgetting the else chain — multiple prints for FizzBuzz numbers.
  • Looping 0..99 (off-by-one).
  • Using a single if/else if ladder but checking % 3 before % 15. The 15-multiples then misclassify as Fizz.

Learning connection

After this, you can read any for-loop + chained conditional in real C without flinching. The discipline of "check the most specific case first" is the same as the case ordering in a real switch or in tighter pattern-matching code.

Why this matters

The most-asked screening question in the world. Get it precise once and it's a freebie forever.

Input format

No input.

Output format

100 lines, one per number 1..100.

Constraints

Check the 15-divisible case first. Inclusive of 100. One printf per iteration.

Starter code

#include <stdio.h>
#include <string.h>

void fizzbuzz(int n, char *out, size_t out_sz) {
    /* TODO */
    out[0] = '\0';
}

Common mistakes

Wrong case order (15 misclassified as Fizz). Off-by-one (100 missing). Forgetting else.

Edge cases to handle

i=15, 30, 45, 60, 75, 90 → FizzBuzz. i=100 must be included (loop bound is <= 100).

Complexity

O(N).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.