FizzBuzz program in java

FizzBuzz Java

In this post, we will see how to program FizzBuzz in java.

Fizzbuzz is a fun game played generally by school children. It is simple game in which when your turn comes, you need to say the next number. So here are rules of the game:

  • If number is divisible by 3, then you need to say Fizz
  • If number is divisible by 5, then you need to say Buzz
  • If number is divisible by 3 and 5 both, then you need to say FizzBuzz
  • Else you just need to say next number

For example:

If there are 20 children then number will be printed as below:

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

Simple FizzBuzz Java solution

Here is simple FizzBuzz java solution

Output of above program will be:

Enter number:
20
The FizzBuzz numbers will be:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz

Java FizzBuzz program using Java 8

Here is the one line solution for Java 8 using Java 8 Stream

Complete java FizzBuzz program

When you run above program, you will get below output:

Enter number:
15
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz

That’s all about FizzBuzz program in java.

Was this post helpful?

Leave a Reply

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