Table of contents
1.
Introduction
2.
Dry Run of FizzBuzz Program in Java  
3.
Algorithm of FizzBuzz Program in Java  
3.1.
Code
4.
Method for FizzBuzz Problem
4.1.
Method 1: Using the Modulo Operator
4.2.
Method 2: Without Using the Modulo Operator
4.3.
Using Java 8
4.4.
Using mapToObj() Method
5.
Time and Space Complexity
6.
Frequently Asked Questions
6.1.
Why is FizzBuzz used in coding interviews?
6.2.
What are the benefits of solving FizzBuzz using Java 8?
6.3.
Which method is the most efficient for solving FizzBuzz?
7.
Conclusion
Last Updated: Mar 17, 2025
Medium

FizzBuzz Program in Java

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The FizzBuzz program is a common coding challenge that tests basic programming logic and control flow. The task is to print numbers from 1 to a given limit, replacing multiples of 3 with "Fizz," multiples of 5 with "Buzz," and multiples of both 3 and 5 with "FizzBuzz." 

FizzBuzz Program in Java

In this article, we will discuss different approaches to implementing the FizzBuzz program in Java, along with code examples for better understanding.

Dry Run of FizzBuzz Program in Java  

Before writing any code, it’s important to understand how the FizzBuzz program works. A dry run helps us visualize the logic & see how the program behaves step by step. Let’s go through the process manually for numbers 1 to 15.  

Here’s how the program should work:  

1. Start from 1 & go up to 15.  
 

2. For each number, check if it’s divisible by 3, 5, or both.  
 

3. Print "Fizz" if divisible by 3, "Buzz" if divisible by 5, & "FizzBuzz" if divisible by both.  
 

4. If none of the above, print the number itself.  


Let’s see how this looks:  
 

1: Not divisible by 3 or 5 → Print `1`.  
 

2: Not divisible by 3 or 5 → Print `2`.  
 

3: Divisible by 3 → Print `Fizz`.  
 

4: Not divisible by 3 or 5 → Print `4`.  
 

5: Divisible by 5 → Print `Buzz`.  
 

6: Divisible by 3 → Print `Fizz`.  
 

7: Not divisible by 3 or 5 → Print `7`.  
 

8: Not divisible by 3 or 5 → Print `8`.  
 

9: Divisible by 3 → Print `Fizz`.  
 

10: Divisible by 5 → Print `Buzz`.  
 

11: Not divisible by 3 or 5 → Print `11`.  
 

12: Divisible by 3 → Print `Fizz`.  
 

13: Not divisible by 3 or 5 → Print `13`.  
 

14: Not divisible by 3 or 5 → Print `14`.  
 

15: Divisible by both 3 & 5 → Print `FizzBuzz`.  
 

This dry run helps us understand the logic clearly. Now, let’s translate this into an algorithm.  

Algorithm of FizzBuzz Program in Java  

Now that we’ve done a dry run, let’s break down the FizzBuzz problem into a step-by-step algorithm. An algorithm is like a recipe—it tells the computer exactly what to do in a logical sequence. Let’s see how we can approach the FizzBuzz problem:  

1. Start a loop from 1 to 100: We need to check each number in this range.  
 

2. Check divisibility rules for each number:  
 

  • If the number is divisible by both 3 & 5, print "FizzBuzz". 
     
  • If the number is divisible by 3 (but not 5), print "Fizz".  
     
  • If the number is divisible by 5 (but not 3), print "Buzz".  
     
  • If none of the above, print the number itself.  
     

3. Repeat the process until the loop reaches 100.  


Let’s write this algorithm in simple steps:  


Step 1: Initialize a loop  

We’ll use a `for` loop to iterate through numbers 1 to 100.  


Step 2: Check divisibility conditions  

Inside the loop, we’ll use `if-else` statements to check the divisibility rules.  


Step 3: Print the result  

Based on the conditions, we’ll print "Fizz", "Buzz", "FizzBuzz", or the number itself.  

Code

public class FizzBuzz {
    public static void main(String[] args) {
        // Loop from 1 to 100
        for (int i = 1; i <= 100; i++) {
            // Check if divisible by both 3 & 5
            if (i % 3 == 0 && i % 5 == 0) {
                System.out.println("FizzBuzz");
            }
            // Check if divisible by 3
            else if (i % 3 == 0) {
                System.out.println("Fizz");
            }
            // Check if divisible by 5
            else if (i % 5 == 0) {
                System.out.println("Buzz");
            }
            // If none of the above, print the number
            else {
                System.out.println(i);
            }
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


In this Code:  

1. Loop: The `for` loop runs from 1 to 100. The variable `i` represents the current number.  
 

2. First Condition: `if (i % 3 == 0 && i % 5 == 0)` checks if the number is divisible by both 3 & 5. If true, it prints "FizzBuzz".  
 

3. Second Condition: `else if (i % 3 == 0)` checks if the number is divisible by 3. If true, it prints "Fizz".  
 

4. Third Condition: `else if (i % 5 == 0)` checks if the number is divisible by 5. If true, it prints "Buzz". 

 

5. Default Case: If none of the above conditions are true, the `else` block prints the number itself.  


This algorithm is simple & efficient. It ensures that all conditions are checked in the correct order, & the program runs smoothly from 1 to 100.  

Method for FizzBuzz Problem

The FizzBuzz problem requires printing numbers from 1 to N, replacing multiples of 3 with "Fizz", multiples of 5 with "Buzz", and multiples of both 3 and 5 with "FizzBuzz".

Problem Statement: Write a Java program to print numbers from 1 to N using the following conditions:

  • Print "Fizz" for numbers divisible by 3.
     
  • Print "Buzz" for numbers divisible by 5.
     
  • Print "FizzBuzz" for numbers divisible by both 3 and 5.
     
  • Print the number itself if it is not divisible by 3 or 5.

Method 1: Using the Modulo Operator

Using the modulo operator (%) is the simplest way to solve the problem.

public class FizzBuzz {
    public static void main(String[] args) {
        int n = 20; // Define the range
        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 == 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);
            }
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz

Method 2: Without Using the Modulo Operator

This method avoids using % by checking divisibility using subtraction.

public class FizzBuzzWithoutModulo {
    public static void main(String[] args) {
        int n = 20;
        int fizz = 0, buzz = 0;
        for (int i = 1; i <= n; i++) {
            fizz++;
            buzz++;  
            if (fizz == 3 && buzz == 5) {
                System.out.println("FizzBuzz");
                fizz = 0;
                buzz = 0;
            } else if (fizz == 3) {
                System.out.println("Fizz");
                fizz = 0;
            } else if (buzz == 5) {
                System.out.println("Buzz");
                buzz = 0;
            } else {
                System.out.println(i);
            }
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

(Same as previous output)

Using Java 8

Java 8 introduced the IntStream class, which allows a more functional programming approach.

import java.util.stream.IntStream;
public class FizzBuzzJava8 {
    public static void main(String[] args) {
        int n = 20;
        IntStream.rangeClosed(1, n)
            .mapToObj(i -> (i % 3 == 0 && i % 5 == 0) ? "FizzBuzz" :
                          (i % 3 == 0) ? "Fizz" :
                          (i % 5 == 0) ? "Buzz" : i)
            .forEach(System.out::println);
    }
}
You can also try this code with Online Java Compiler
Run Code

Using mapToObj() Method

We can further simplify Java 8 implementation by using the mapToObj() method.

import java.util.stream.IntStream;
public class FizzBuzzMapToObj {
    public static void main(String[] args) {
        IntStream.rangeClosed(1, 20)
            .mapToObj(i -> i % 3 == 0 && i % 5 == 0 ? "FizzBuzz" :
                          i % 3 == 0 ? "Fizz" :
                          i % 5 == 0 ? "Buzz" : String.valueOf(i))
            .forEach(System.out::println);
    }
}
You can also try this code with Online Java Compiler
Run Code

Time and Space Complexity

MethodTime ComplexitySpace Complexity
Using ModuloO(N)O(1)
Without ModuloO(N)O(1)
Java 8 (Streams)O(N)O(N)
Using mapToObj()O(N)O(N)

 

  • The traditional approach using loops and modulo operator has O(N) time complexity with O(1) space complexity.
     
  • Java 8 stream-based solutions may consume extra memory (O(N) space complexity) due to stream operations.

Frequently Asked Questions

Why is FizzBuzz used in coding interviews?

FizzBuzz tests basic programming skills, logic building, and problem-solving ability. It helps assess how well candidates write clean and efficient code.

What are the benefits of solving FizzBuzz using Java 8?

Java 8 provides a more readable and functional programming approach using streams, reducing boilerplate code.

Which method is the most efficient for solving FizzBuzz?

The traditional loop with a modulo operator is the most efficient in terms of time and space complexity.

Conclusion

In this article, we discussed different ways to implement the FizzBuzz program in Java. We started with the basic modulo approach, then implemented it without modulo, and later discussed Java 8’s stream-based solutions. This problem is crucial for improving coding logic and is frequently asked in interviews. Understanding different approaches to solving FizzBuzz enhances problem-solving skills in Java programming.

Live masterclass