beginnerLogical ThinkingJava

FizzBuzz Problem

Print the numbers 1 to N, substituting Fizz, Buzz, or FizzBuzz for the multiples of 3, 5, or both.

Quick Answer

Loop from 1 to N. For each number, check divisibility by 3 and 5. Print FizzBuzz when it is divisible by both, Fizz when only by 3, Buzz when only by 5, and the number itself otherwise. Test the both-case first so it is not missed.

Problem Statement

Print every integer from 1 to N, one per line, with these substitutions: print Fizz instead of numbers divisible by 3, Buzz instead of numbers divisible by 5, and FizzBuzz instead of numbers divisible by both 3 and 5.

All other numbers are printed as-is. For example, with N = 5 the output is 1, 2, Fizz, 4, Buzz.

Input: A single positive integer N.

Output: N lines, each being a number, Fizz, Buzz, or FizzBuzz.

Examples

Example 1
Input:  5
Output: 1 2 Fizz 4 Buzz

3 is a multiple of 3 (Fizz) and 5 is a multiple of 5 (Buzz); the rest print as numbers.

Example 2
Input:  15
Output: 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz

15 is divisible by both 3 and 5, so it prints FizzBuzz.

Constraints

  • 1 <= N <= 100000
  • A number divisible by both 3 and 5 must print FizzBuzz, not Fizz or Buzz

Think Before You Code

Reveal the questions to ask yourself first
  • Which divisibility case must you check first so you never miss FizzBuzz?
  • What operator tells you a number is an exact multiple of another?
  • How do the three conditions (by 3, by 5, by both) fit into an if/else chain?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
A number `i` is divisible by 3 when `i % 3 == 0`, and by 5 when `i % 5 == 0`.
Hint 2
Check the FizzBuzz case (divisible by 3 AND 5) before the individual Fizz and Buzz cases.
Hint 3
Use one if/else-if/else chain per number: FizzBuzz, then Fizz, then Buzz, then the number itself.

Approach

Reveal the step-by-step approach

Iterate once from 1 to N and classify each value.

  1. Loop i from 1 through N.
  2. For each i:
    • If i % 15 == 0 (divisible by both 3 and 5), print FizzBuzz.
    • Else if i % 3 == 0, print Fizz.
    • Else if i % 5 == 0, print Buzz.
    • Else print i.

Checking i % 15 == 0 first is the key: a number like 15 is divisible by 3 and by 5, so the combined case must win before the single-factor checks.

Dry Run

Walk through the example step by step

Running FizzBuzz for N = 5:

i | %15==0 | %3==0 | %5==0 | printed
--+--------+-------+-------+--------
1 | no     | no    | no    | 1
2 | no     | no    | no    | 2
3 | no     | yes   | -     | Fizz
4 | no     | no    | no    | 4
5 | no     | no    | yes   | Buzz

Solution

Reveal the full Java solution
public class FizzBuzz {
    public static void fizzBuzz(int n) {
        for (int i = 1; i <= n; 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);
            }
        }
    }

    public static void main(String[] args) {
        fizzBuzz(5);
        // Prints:
        // 1
        // 2
        // Fizz
        // 4
        // Buzz
    }
}

The single divisibility-by-15 test captures "divisible by both 3 and 5" cleanly, because a number is a multiple of both 3 and 5 exactly when it is a multiple of their least common multiple, 15. Ordering that test first prevents 15, 30, 45 and so on from being caught early by the Fizz branch.

The loop runs in a straight line with a constant amount of work per number, so the output is produced in a single pass.

Time: O(n)Space: O(1)

Common Mistakes

  • Checking `i % 3` before the combined case, so multiples of 15 wrongly print Fizz.
  • Using separate independent ifs without else, printing both Fizz and Buzz on their own lines for 15.

Edge Cases to Test

  • N = 1 prints just the number 1.
  • N = 3 prints 1, 2, Fizz.
  • N = 15 must include FizzBuzz exactly once, at position 15.

Interview Follow-Ups

  • How would you return the results as a List<String> instead of printing them?
  • How would you generalise to custom words and divisors passed in as parameters?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us