Java FizzBuzz Program

In this article, we’ll talk about what is FizzBuzz and how to implement FizzBuzz in Java.

What is FizzBuzz?

FizzBuzz is a group game for children to understand the concept of division and multiplication. In which, each child counts the numbers (starting from 1) following these rules:

  1. if that number is divisible by ‘3’ then replace the number by word fizz.
  2. If that number is divisible by ‘5’ then replace the number by word buzz.
  3. In case that number is divisible with both the numbers then replace the number by word fizzbuzz.

So, if any child hesitates or makes a mistake then they have to leave the game.

Note: We can use any two numbers instead of 3 and 5.

Java FizzBuzz Program

Let’s see the implementation of Fizz buzz for first 100 numbers –

Output:

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz …… till 100

Explanation:

In above program, we’re starting a loop from 1 to 100. Then we’re having three conditions:

  1. if i is divisible by both 3 and 5, then print FizzBuzz. If no then go for second condition.
  2. if i is divisible by 3 then print Fizz otherwise go for condition 3.
  3. if i is divisible by 5, print Buzz otherwise the number will be printed.

This question is one of the most asked questions in coding interviews.

Comment down below if you have any queries regarding java fizzbuzz implementation.

Leave a Reply

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