Fizzbuzz Program in Java

In this article, Our main topic of discussion is going to be Fizzbuzz Program in Java. Starting the article with getting an idea of what the fizzbuzz problem is about, we will be looking at the dry run of the fizzbuzz program in java, the algorithm followed to do it while in the further stages of this article, we will look at the code as well as analysis of the fizzbuzz program in java.

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
We studied how to solve the fizzbuzz program in java using two approaches, one using the modulo operator and the other without using it. Dry run, flowchart, algorithm, code and analysis of code were discussed in the article.

We hope you liked the article on the fizzbuzz program in java. We expect to see you at PrepBytes with another informative article soon.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java
Leap Year Program in Java
Array Programs in Java
Linked List Program in Java
String Programs in Java
Star Program in Java
Number Pattern Program in Java
For Loop Program In Java
Pattern Program in Java
String Palindrome Program in Java
Thread Program in JAVA
Java Scanner Program
While Loop Program in Java
Bubble Sort Program in Java
Fibonacci Series Program in Java
Calculator Program in Java

Leave a Reply

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