FizzBuzz Program in Java - Interview Question
What is FizzBuzz?
In this question, you will work with two numbers 3 and 5. The user gives a number as input and then we'll have to get the list of all the numbers starting from 1 till the given number. Now, all we need to do is to find the numbers that are multiples of 3 and 5 and print special words called Fizz and Buzz instead of those multiples respectively. If a number is multiple of both 3 as well as 5 (say 15) FizzBuzz should be printed and if a number is neither a multiple of 3 nor 5 then the number itself is printed. Now let us see how this works.
public static void main(String args)
Scanner s=new Scanner(System.in);
System.out.println("Enter the number");
Enter the number
To know the multiples of a number we'll have to find out the remainder and it should be 0. The same logic is used there.
if(i%5==0)means if a number is multiple of 5 then print Buzz.
if(i%3==0)means if a number is multiple of 3 then print Fizz.
if((i%3==0)&&(i%5==0))means if a number is multiple of 3 as well as 5, then print FizzBuzz.
That's it, this is a way in which we can outsmart this most famous interview question, the FizzBuzz