Tuesday, 5 February 2019

10. FizzBuzz Program in Java - Interview Question


Description:
Write a sample code to FizzBuzz Program in Java.


Code:
?
1
2
3
4
5
6
7
8
9

import java.util.Scanner; public class FizzBuzz { public static void main(String[] args) { for(int i=1; i<=15; i++) { Scanner s=new Scanner(System.in); if(i%(3*5)==0)System.out.println("fizzBuzz"); else if (i% 5 ==0) System.out.println("fizz"); else if (i% 3==0)System.out.println("Buzz"); else System.out.println(i); } } }


Output:
1
2
Buzz
4
fizz
Buzz
7
8
Buzz
fizz
11
Buzz
13
14
fizzBuzz

No comments:

Post a Comment