Do you think IIT Guwahati certified course can help you in your career?
No
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."
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
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.
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
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.