Java Tutorial
🔍

FizzBuzz in Java

beginner·  Control Flow  ·  Loops

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

Input
1–30
Output
1, 2, Fizz, 4, Buzz, ...

Java Program

Java
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

1 2 Fizz 4 Buzz Fizz 7 ... FizzBuzz

Core Logic

The modulo operator does all the heavy lifting here — check divisibility by 15 first, then fall back to 3 and 5.

How It Works
  1. 1A for loop counts i from 1 to 30.
  2. 2i % 15 == 0 is checked first — 15 is 3 × 5, so this catches numbers divisible by both before the individual checks run.
  3. 3i % 3 == 0 catches remaining multiples of 3 and prints "Fizz".
  4. 4i % 5 == 0 catches remaining multiples of 5 and prints "Buzz".
  5. 5Anything left over is just printed as the number itself.
At 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

for loopmodulo operatorif / else if

Approach 2: StringBuilder (Independent Conditions)

Java
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

1 2 Fizz 4 Buzz Fizz 7 ... FizzBuzz

Core Logic

Instead of special-casing 'divisible by both', let 'Fizz' and 'Buzz' get appended independently — the combination takes care of itself.

How It Works
  1. 1An empty StringBuilder is created fresh for every number.
  2. 2if (i % 3 == 0) appends "Fizz" — independently of whether 5 also divides i.
  3. 3if (i % 5 == 0) appends "Buzz" in the same way, so a multiple of both ends up with "FizzBuzz" naturally.
  4. 4If neither condition added anything, output.length() > 0 is false, so the number itself is printed instead.
At 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.

Key Concepts

StringBuilderindependent conditionsfor loop

Related Programs