Home » Blog » Write a Java program using class that prints the numbers 1 to 50. For all multiples of 3 print “Fizz” and for all multiples of 5 print “Bizz”. For multiples of both 3 and 5 print “Fizz-Bizz”.

Write a Java program using class that prints the numbers 1 to 50. For all multiples of 3 print “Fizz” and for all multiples of 5 print “Bizz”. For multiples of both 3 and 5 print “Fizz-Bizz”.

In this program, we iterate the loop and print Fizz when the current number is multiple of 3, and print Bizz when the current number is multiple of 5, and print Fizz-Bizz when the current number is multiple of 3 and 5.

The below example illustrate this.

  • Example
class FizzBizzDemo
{
	//print fizz when number is multiply by 3 and bizz when number is multiply by 5 and for both print fizz-bizz
	public static void main(String args[])
	{
		int i;
		for(i=0;i<=50;i++)
		{
			if(i%3==0 && i%5==0)
			{
				System.out.println(i+" - Fizz-Bizz");
			}
			else
			{
				if(i%3==0)
				{
					System.out.println(i+" - Fizz");
				}
				else if(i%5==0)
				{
					System.out.println(i+" - Bizz");
				}
				else
				{
					System.out.println(i+" - ");
				}
			}
		}
	}
}
  • Output
0 - Fizz-Bizz
1 -
2 -
3 - Fizz
4 -
5 - Bizz
6 - Fizz
7 -
8 -
9 - Fizz
10 - Bizz
11 -
12 - Fizz
13 -
14 -
15 - Fizz-Bizz
16 -
17 -
.
.
45 - Fizz-Bizz
46 -
47 -
48 - Fizz
49 -
50 - Bizz

 

Leave a Reply

Your email address will not be published.