FizzBuzz

1 min readUpdated August 29, 2026core-javaexamplesbeginner

FizzBuzz is a small exercise that’s popular for practicing basic control flow: loops, conditionals, and the modulo operator.

The rules

For each number from 1 to 100:

  • If it’s divisible by both 3 and 5, print FizzBuzz.
  • If it’s divisible by 3, print Fizz.
  • If it’s divisible by 5, print Buzz.
  • Otherwise, print the number itself.
FizzBuzz.java
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
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);
}
}
}
}

Try it yourself: the highlighted lines (4–11) are the whole algorithm — try changing 15 to a different combined divisor, or adding a third rule (e.g. multiples of 7 print Bazz).

Why check divisible-by-15 first?

15 is divisible by both 3 and 5, so if you checked i % 3 == 0 first, numbers like 15 and 30 would print Fizz and never reach the FizzBuzz branch. Checking the combined case (% 15) first avoids that ordering bug.