In this
article, we will see FizzBuzz Program in Java. FizzBuzz is a
most frequently asked question in interview. But, it is very simple.
In FizzBuzz program, we print a series of positive
integers from 1 to n. If any number is completely divisible by 3 and 5, then
print "FizzBuzz" and if any number is only completely divisible by 3,
then print "Fizz" and if any number is only completely divisible by
5, then prints "Buzz", otherwise prints that positive integer.
FizzBuzz Program in Java
import java.util.*;
class FizzBuzz
{
public static void main(String[] args) {
System.out.print("Enter the terms (in numbers):");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(int i=1;i<=n;i++)
{
if((i%3==0)&&(i%5==0))
System.out.println("FizzBuzz");
else if(i%3==0)
System.out.println("Fizz");
else if(i%5==0)
System.out.println("Buzz");
else
System.out.println(i);
}
}
}
Output:
Enter the terms (in numbers):20
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
0 comments:
Post a Comment