FizzBuzz in Java
Problem
FizzBuzz is a classic warm-up exercise built around checking divisibility with the modulo operator.
Print the numbers 1 through 30, replacing multiples of 3 with 'Fizz', multiples of 5 with 'Buzz', and multiples of both with 'FizzBuzz'.
Java Program
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 30; i++) {
// Check the combined case first so multiples of both aren't caught by % 3 or % 5 alone
if (i % 15 == 0) System.out.println("FizzBuzz");
else if (i % 3 == 0) System.out.println("Fizz");
else if (i % 5 == 0) System.out.println("Buzz");
else System.out.println(i);
}
}
}Output
Core Logic
The modulo operator does all the heavy lifting here — check divisibility by 15 first, then fall back to 3 and 5.
- 1A for loop counts
ifrom 1 to 30. - 2
i % 15 == 0is checked first — 15 is 3 × 5, so this catches numbers divisible by both before the individual checks run. - 3
i % 3 == 0catches remaining multiples of 3 and prints"Fizz". - 4
i % 5 == 0catches remaining multiples of 5 and prints"Buzz". - 5Anything left over is just printed as the number itself.
i = 15, the % 15 check fires first, printing "FizzBuzz" instead of falling through to the % 3 or % 5 branches.Key Point: Ordering the % 15 check before % 3 and % 5 is what prevents 'FizzBuzz' from being printed as just 'Fizz'.
Key Concepts
Approach 2: StringBuilder (Independent Conditions)
public class FizzBuzzStringBuilder {
public static void main(String[] args) {
for (int i = 1; i <= 30; i++) {
StringBuilder output = new StringBuilder();
if (i % 3 == 0) output.append("Fizz"); // independent check for 3
if (i % 5 == 0) output.append("Buzz"); // independent check for 5
// If neither condition matched, fall back to printing the number
System.out.println(output.length() > 0 ? output.toString() : String.valueOf(i));
}
}
}
Output
Core Logic
Instead of special-casing 'divisible by both', let 'Fizz' and 'Buzz' get appended independently — the combination takes care of itself.
- 1An empty
StringBuilderis created fresh for every number. - 2
if (i % 3 == 0)appends"Fizz"— independently of whether 5 also dividesi. - 3
if (i % 5 == 0)appends"Buzz"in the same way, so a multiple of both ends up with"FizzBuzz"naturally. - 4If neither condition added anything,
output.length() > 0is false, so the number itself is printed instead.
i = 15, both if blocks run, appending "Fizz" then "Buzz", so output becomes "FizzBuzz" without a dedicated % 15 check.Key Point: This scales better if more rules were added later (like '% 7 → Bazz') — each rule is an independent if, instead of needing a new combined-case branch for every pair of rules.