Java

What is the FizzBuzz Problem in Java

In basic Java programming, there can be instances where one wants to enhance programming and logical skills. More specifically, enabling the newbies to create a somewhat tricky algorithm or make logic. Also, to retrieve some filtered values based on the requirement. In such cases, the “FizzBuzz” problem in Java assists beginners in enhancing their coding skills.

This article will demonstrate the rules and implementation of the “FizzBuzz” problem in Java.

What is the “FizzBuzz” Problem in Java?

The “FizzBuzz” problem corresponds to a game in real life that is played by a group of people having the following rules:

  • The sequence of turns is decided before the game is started.
  • The first player initiates by specifying any number, i.e., “1”.
  • The next player says the next number, and so on.
  • The tricky part is that the word “Fizz” is said instead of the number being multiple of “3”.
  • Likewise, the word “Buzz” is said if it(number) is a multiple of “5”.
  • However, if the number is a multiple of both the numbers “3” and “5”, the word “FizzBuzz” is used instead.

Note: The rules for the numbers specified above can change, i.e., “11” and “13” etc.

How to Implement the “FizzBuzz” Problem in Java?

To implement the “FizzBuzz” problem in Java, apply the following approaches:

The following approaches will apply a check upon the numbers “3” and “5” and invoke the corresponding word.

Approach 1: Implementing the “FizzBuzz” Problem in Java Using “if/else” Statement

The “if/else” statement checks for a particular condition and invokes the corresponding statement. This statement can be applied to apply a check upon the occurrence of the numbers in accordance with the rules.

Example

Let’s overview the below-provided example:

public class fizzbuzz {

public static void main( String args[] ) {

for(int i = 1; i <= 20; 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);

}}

}}

According to the above code, apply the following steps:

  • Firstly, apply a “for” loop to iterate along the integers till “20”.
  • Within this range, check for the numbers being multiple of both “3” and “5” by checking if they are completely divisible and no remainder is left behind.
  • Upon the satisfied condition, log the word “FizzBuzz” in accordance with the rules.
  • Similarly, upon the occurrence of the numbers being multiple of “3”, log the word “Fizz”.
  • Likewise, display the word “Buzz” if the number is a multiple of “5”.
  • In the “else” condition, the number itself will be displayed on the console.

Output

In this output, it can be observed that the corresponding words replace the numbers that are multiples of “3”, “5” or both.

Approach 2: Implementing the “FizzBuzz” Problem in Java Using the “Ternary Operator”

The “Ternary” operator also checks for a particular condition and invokes the corresponding block based on the satisfied or unsatisfied conditions. This operator can be utilized combined with the “valueOf()” method to likewise check for the divisibility condition and log the corresponding outcome as a string.

Syntax

(condition) ? (invoke if true) : (invoke if false);

Example

Let’s go through the following example to understand the stated concept:

public class Fizzbuzz2 {

public static void main( String args[] ) {

for(int i = 1; i <= 20; i++) {

String output = (i % 3 == 0 ? (i % 5 == 0 ? "FizzBuzz" : "Fizz") :

  (i % 5 == 0 ? "Buzz" : String.valueOf(i)));

System.out.println(output);

}

}}

According to this code snippet, perform the following steps:

  • Likewise, include a “for” loop to iterate along the integers till the specified limit.
  • Within this range, check for the condition such that if the number is a multiple of “3”, invoke the former condition after “?”. If this invoked condition(multiple of 5) also becomes satisfied, log the word “FizzBuzz”.
  • In the case of only the former satisfied condition, return the word “Fizz”.
  • Otherwise, access the latter block in the next line. Here, similarly, check for the former satisfied condition and return the word “Buzz”.
  • In the other case, log the value itself as a string via the associated “valueOf()” method.
  • Finally, log the corresponding outcome upon each iteration.

Output

Before proceeding to the next approach, make sure to include the following packages to enable user input and manipulate “int” values, respectively.

import java.util.Scanner;

import java.util.stream.IntStream;

Approach 3: Implementing the “FizzBuzz” Problem in Java Using the “mapToObj()” and “rangeClosed() Methods

The “mapToObj()” method of the “IntStream” interface is utilized to return an object-valued stream and the “rangeClosed()” method returns a sequential ordered IntStream. These methods can be applied in combination with the “Ternary” operator to return the corresponding word instead of the integer within the user-input range.

Syntax

static IntStream rangeClosed(int first, int last)

In this syntax:

  • first” refers to the value including the first value.
  • last” points to the last value.

Example

Consider the below-stated example:

public class fizzbuzz3 {

 public static void main( String args[] ) {

  Scanner input = new Scanner(System.in);

System.out.print("Input the end limit:\n");

  int inInteger = input.nextInt();

IntStream.rangeClosed(1, inInteger).mapToObj(i->i%3 == 0?(i%5 == 0? "FizzBuzz":"Fizz "):(i%5 == 0? "Buzz ": i+" ")).forEach(System.out::print);

input.close();

}}

In the above lines of code, perform the following steps:

  • Firstly, create a “Scanner” object via the “new” keyword and the “Scanner()” constructor, respectively.
  • The “System.in” parameter reads the input.
  • After that, associate the “nextInt()” method to get the input as an integer.
  • Now, associate the “rangeClosed()” method with the “IntStream” interface and specify the range by referring to the user-input value.
  • Also, apply the “mapToObj()” method along with the ternary operator to return an object value stream.
  • Note: The functionality performed in the ternary operator is identical to the previous approach.
  • Lastly, apply the “forEach()” method to display all the values and close the scanner via the “close()” method.

Output

This outcome implies that the sequential “IntStream” is returned in accordance with the input range.

Conclusion

To implement the “FizzBuzz” problem in Java, apply the “if/else” statement, the “Ternary” operator, or the “mapToObj()” and “rangeClosed()” methods. These approaches apply a check upon the specified or user-input range of integers and return the corresponding word in accordance with the game, i.e., “FizzBuzz” rules. This blog is guided to discuss the “FizzBuzz” problem in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.