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

By Saruque Ahamed Mollick

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

Leave a Reply

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