In this tutorial we will learn BuzzFizz Program in Java. The concept of this BuzzFizz program is the multiples of 3 will replacw with "Fizz", the multiples of 5 will replace with "Buzz" and the multiples of both 3 and 5 will replace with "FizzBuzz".In this program we are going to use the for loop and if,else and else if statement. This type of program are often asked in a job interview.
BuzzFizz Program in Java
class BuzzFizz
{
public static void main(String arg[])
{
int no, i;
Scanner s=new Scanner(System.in);
System.out.println("Enter range of numbers");
no=s.next();
for(i=1; i<=no; i++)
{
if((i % (3*5)) == 0)
{
System.out.println("FizzBuzz\n");
}
else if ((i % 5) == 0)
{
System.out.println("Buzz\n");
}
else if ((i % 3) == 0)
{
System.out.println("Fizz\n");
}
else
{
System.out.println(i);
}
}
}
}
Output
Enter Range of number: 20
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz