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:
- If a number is a multiple of 3, print
Fizzinstead. - If it’s a multiple of 5, print
Buzzinstead. - If it’s a multiple of both 3 and 5, print
FizzBuzz. - Otherwise, just print the number.
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 — $27Step 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.
Related Articles
- C++ Modulo Operator — the
%operator that powers FizzBuzz - C++ Conditionals Tutorial — if, else if, and else explained
- C++ Loops Tutorial — for and while loops in depth
- C++ Even or Odd Program — another modulo classic
- C++ Ternary Operator — tidy up small conditionals
- C++ Programs for Beginners — 40+ more example programs, easiest to hardest
Take Your C++ Further

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 — $2787 pages · plain English · 30-day money-back guarantee