Skip to content
C++ Better Explained
Go back
FizzBuzz in C++: The Classic Beginner Problem Explained Step by Step

FizzBuzz in C++

FizzBuzz is the most famous beginner programming exercise — and a question interviewers really do ask to check you can write a simple loop. The task sounds trivial, but it quietly tests loops, conditionals, and the modulo operator all at once. Let’s build it from scratch.


The Rules

Print every number from 1 to 100, with three twists:

That’s it. The whole challenge is turning those four rules into code in the right order.


Step 1: Loop From 1 to 100

Before worrying about Fizz or Buzz, get a loop printing the plain numbers:

#include <iostream>

int main() {
    for (int i = 1; i <= 100; ++i) {
        std::cout << i << "\n";
    }
    return 0;
}

This prints 1 through 100, one per line. Now we just need to replace some of those numbers with words.

Ready for the step after small programs? The jump from exercises like this to real projects is pointers, memory and OOP. C++ Better Explained covers all three in plain English, with analogies and code diagrams.

Get the book — $27

Step 2: Test Divisibility With Modulo

The modulo operator % gives the remainder after division. If the remainder is zero, the number divides evenly. So i % 3 == 0 means “i is a multiple of 3.” That single test is the heart of FizzBuzz.

The trick is the order of your checks. A multiple of both 3 and 5 is a multiple of 15, so you must test for 15 first. If you don’t, i % 3 == 0 will match first and you’ll print Fizz when you wanted FizzBuzz.


The Complete FizzBuzz Program

Putting the loop and the ordered checks together:

#include <iostream>

int main() {
    for (int i = 1; i <= 100; ++i) {
        if (i % 15 == 0)
            std::cout << "FizzBuzz\n";
        else if (i % 3 == 0)
            std::cout << "Fizz\n";
        else if (i % 5 == 0)
            std::cout << "Buzz\n";
        else
            std::cout << i << "\n";
    }
    return 0;
}

Because these are else if branches, only the first matching one runs — which is exactly why checking 15 first works. Run it and you’ll see 1 2 Fizz 4 Buzz Fizz 7 ... all the way to 100.


A Cleaner Version Without Checking 15

There’s an elegant alternative that avoids the “test 15 first” rule entirely. Build up a string: add Fizz for a multiple of 3, add Buzz for a multiple of 5, and if nothing was added, use the number itself.

#include <iostream>
#include <string>

int main() {
    for (int i = 1; i <= 100; ++i) {
        std::string output;
        if (i % 3 == 0) output += "Fizz";
        if (i % 5 == 0) output += "Buzz";
        if (output.empty()) output = std::to_string(i);
        std::cout << output << "\n";
    }
    return 0;
}

For a multiple of 15, both if checks fire, so output becomes "FizzBuzz" automatically. This version is easy to extend — add a Bazz rule for multiples of 7 and you don’t have to rethink any combined cases.


Common Mistakes

The number-one bug is checking 3 or 5 before 15 — you’ll never see FizzBuzz. The second is using = (assignment) instead of == (comparison) inside the if. And remember % only works on integers; it’s perfect here since every value is a whole number.



Take Your C++ Further

C++ Better Explained ebook cover

Small programs like this one are the right way to practise. The next step is the part most beginners get stuck on: pointers, memory and OOP. C++ Better Explained covers all three in 87 plain-English pages, with analogies and code diagrams.

Get the book — $27

87 pages · plain English · 30-day money-back guarantee

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
How to Find an Element in a Vector in C++ (std::find)
Next Post
Nested Loops in C++: Patterns, Grids, and the Multiplication Table

Keep Learning