# FizzBuzz Problem In Java- A java code to solve FizzBuzz problem

Before going to solve this problem let us know about the problem

How to replace String Characters In Java Using replace() Method?

## FizzBuzz Problem

The FizzBuzz is an ancient word game. It is started to teach the children about division.
Now in many coding tests, it is a very popular challenge to solve the FizzBuzz problem.
In this problem, you have to print numbers from 0 to a certain range. But the problem is that you must have to print “Fizz” instead of the numbers which are divisible by 3 and “Buzz” instead of the numbers which are divisible by 5. But if a number is found to be divisible by both 3 and 5 then you must have to print “FizzBuzz” instead of the number.
Now let’s solve the FizzBuzz problem in Java

```import java.util.*;

public class Codespeedy{
public static void main(String[] args) {

for(int i=0;i<=50;i++) {
if(i%3==0 && i%5==0) System.out.println("FizzBuzz");
else
if(i%5==0) System.out.println("Buzz");
else
if(i%3==0) System.out.println("Fizz");
else
System.out.println(i);

}
}
}```

Guess The Number Game Using Java with Source Code

Output:

FizzBuzz
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
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz

### 2 responses to “FizzBuzz Problem In Java- A java code to solve FizzBuzz problem”

1. Winciaten says:

Exactly the solution, a job interviewer or whoever doesn’t want to see.
Not extensible at all if more words than “Fizz” and “Buzz” have to be added.
One acceptable solution would be:

`for (int i = 0; i <= 50; i++) { String output = ""; if (i % 3 == 0) output += "Fizz"; if (i % 5 == 0) output += "Buzz"; if (output.isEmpty()) output = Integer.toString(i); System.out.println(output); }`

and it would be even better to work with a Map and iterate through the entries.

• Saruque Ahamed Mollick says:

Thanks for your solution. It is much better.