Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Fizzbuzz Program in Java

Last Updated on June 6, 2023 by Mayank Dham

Welcome to the world of Fizzbuzz! Fizzbuzz is a popular counting game that has become a well-known problem in the realm of programming. It serves as a fundamental exercise to test one’s logical thinking and problem-solving skills, making it a common question asked during interviews and coding assessments. In this article, we will explore the concept of Fizzbuzz, its rules, and its significance in assessing programming abilities. We will delve into the dry run of a Fizzbuzz program in Java, examine the algorithm behind it, and present code examples using both the modulo operator and a counter approach. Whether you are a novice programmer looking to grasp the basics of logic or an experienced developer seeking to refresh your problem-solving skills, this article will provide you with valuable insights into the world of Fizzbuzz. So let’s dive in and unravel the secrets of this intriguing counting game!.

What is Fizzbuzz?

Fizzbuzz is based on the Game of Counting where a number is decided to count up among two or more individuals where any number that is a multiple of 3, including 3 itself must be replaced with Fizz, a number that is a multiple of 5, including 5 itself must be replaced with Buzz and a number that is a multiple of 3 and 5 both must be replaced with the term “FizzBuzz” with the rest of the numbers being spoken naturally.

The fizzbuzz Problem is one of the most famous interview problems to check if the interviewee has sound knowledge of programming logic and problem-solving abilities irrespective of the programming language it is being implemented in. The reason it is taught to novices is to help them with the basics of logic building.

Dry Run of FizzBuzz Program in Java

Now that we have some clarity, looking at the Game of Counting that goes by the name of FizzBuzz. Let us have a look at the dry run of the fizzbuzz program in java.

Let’s suppose the input taken from the user is 20, which is the endpoint set upto which the program must run.

1 and 2 are printed as themselves. Fizz gets printed in place of 3. 4 is printed. Buzz is printed in place of 5. Fizz is printed in place of 6. 7,8 are printed. Fizz is printed in place of 9. Buzz is printed in place of 10. 11 is printed. Fizz is printed in place of 12. 13,14 are printed. FizzBuzz is printed in place of 15. 16,17 are printed. Fizz is printed in place of 18. 19 is printed. Buzz is printed in place of 20.

Algorithm of Fizz Buzz Program in Java

The illustration mentioned below is a flowchart that depicts the triplets of conditions that are going to be executed until the counter exceeds the input set up until which the program is going to run. Now looking at the algorithm of the program, it can be stated as:

The algorithm to fizzbuzz is concise and straightforward to understand. Whenever we get a multiple of 3 and 5, we print FizzBuzz, a multiple of 3 will result in Fizz and a multiple of 5 will get Buzz printed instead. In a scenario where no condition is satisfied then the number at the index is going to be printed

However, there are two methods to solve the fizzbuzz program in java, as discussed in the article; the first uses a modulo and the second where we do not use a modulo.

Algorithm of Fizzbuzz Program in Java using modulo:

  1. Take N as an input from the user
  2. Run a loop from i from 1 to n
    • Print “FizzBuzz” if i%3==0 and i%5==0
    • else Print “Buzz” if i%5 == 0
    • else Print “Fizz” if i%3==0
    • else print i
  3. End of program

Algorithm of Fizzbuzz Program in Java using modulo:

  1. Take N as an input from the user
  2. Set both count3 and count5 as 1,1
  3. Run a loop from i from 1 to n
    • if i%3==0 and i%5==0
      • Print “FizzBuzz”
      • Set count3 and count5 to 0,0
    • else if i%5 == 0
      • Print “Buzz”
      • Set count5 to 0
    • else if i%3 == 0
      • Print “Fizz”
      • Set count3 to 0
    • Else print i
      • Increment count3 and count5 by 1
  4. End of program

Code for Fizzbuzz Program in Java

Let us look at the code using both the approaches discussed for the fizzbuzz program in java.

Method 1: Using the modulo operator

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();
    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);
      }
    }
  }
}

Explanation – Taking n as the input number from the user and later on, a loop is made to run from 1 to n with conditions for printing put in place. Code link and snippets are provided to help understand better.

Output

20
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

import java.util.Scanner;

public class FizzBuzz {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int n = scanner.nextInt();
    int count3 = 1;
    int count5 = 1;

    for (int i = 1; i <= n; i++) {
      if (count3 == 3 && count5 == 5) {
        System.out.println("FizzBuzz");
        count3 = 0;
        count5 = 0;
      } else if (count3 == 3) {
        System.out.println("Fizz");
        count3 = 0;
      } else if (count5 == 5) {
        System.out.println("Buzz");
        count5 = 0;
      } else {
        System.out.println(i);
      }
      count3++;
      count5++;
    }
  }
}

Explanation – In this the modulo was tweaked and two counter variables for 3 and 5 are taken instead, whenever the variable for 3 amounts to three, it is reset to 0, printing Fizz and when the variable for 5 amounts to 5, Buzz is printed with counter reset to 0 and when both happen all together, FizzBuzz is printed with both reset to 0.

Output

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

Analysis of the FizzBuzz Program in Java

The loop for counting runs until the input is provided by the user, hence the time complexity is linear. The worst-case time complexity of the program is O(n).

Speaking of space, no extra auxiliary space is being consumed in the program, therefore we can conclude that the space complexity is constant. i.e. O(1).

Conclusion
In conclusion, Fizzbuzz is a popular counting game used to test programming logic and problem-solving skills. It involves replacing numbers that are multiples of 3 with "Fizz," numbers that are multiples of 5 with "Buzz," and numbers that are multiples of both 3 and 5 with "FizzBuzz." The game is often used as an interview problem to assess a candidate’s understanding of programming concepts. This article provided a dry run of the Fizzbuzz program in Java, explaining the algorithm and providing code examples using both the modulo operator and a counter approach. The modulo operator approach checks divisibility by 3 and 5 using the modulo operator, while the counter approach tracks the multiples of 3 and 5 separately. The time complexity of the Fizzbuzz program is linear, as it runs until the user-defined input is reached. The worst-case time complexity is O(n), where n is the input number. The space complexity is constant (O(1)), as no additional auxiliary space is used in the program.
By understanding the Fizzbuzz program and its implementation in Java, developers can strengthen their problem-solving abilities and gain a solid foundation in programming logic.

Frequently Asked Questions (FAQs)

Q1. What is the purpose of Fizzbuzz in programming interviews?
Fizzbuzz is often used as an interview question to assess a candidate’s programming logic and problem-solving abilities. It helps evaluate their understanding of basic control structures, such as conditionals and loops, and their ability to translate requirements into executable code.

Q2. Can Fizzbuzz be solved using languages other than Java?
Absolutely! Fizzbuzz is a language-agnostic problem, meaning it can be solved using any programming language. The logic behind Fizzbuzz remains the same, regardless of the language you choose. It’s a great exercise to practice your coding skills in any language you’re comfortable with.

Q3. Is Fizzbuzz suitable only for beginners in programming?
While Fizzbuzz is often introduced to beginners due to its simplicity, it can still be a useful exercise for programmers at any level. It serves as a quick warm-up or code refresher, and it can be modified or extended to make it more challenging and interesting for experienced developers.

Q4. Are there alternative approaches to solving Fizzbuzz?
Yes, there are multiple approaches to solving Fizzbuzz. The most common approach is using the modulo operator (%), but you can also implement it using counters or even recursion. Each approach has its own advantages and trade-offs, so it’s worth exploring different methods to enhance your problem-solving skills.

Q5. What are some practical applications of Fizzbuzz in real-world programming?
While Fizzbuzz itself may not have direct practical applications, it helps build fundamental skills that are applicable in various programming scenarios. The ability to break down a problem into smaller logical steps, implement control structures, and validate your code’s correctness are skills that transfer to real-world programming tasks and projects. Fizzbuzz serves as a stepping stone for developing these essential programming abilities.

Leave a Reply

Your email address will not be published. Required fields are marked *