-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathFizzBuzzJava.java
More file actions
33 lines (31 loc) · 881 Bytes
/
FizzBuzzJava.java
File metadata and controls
33 lines (31 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* Program that prints Fizz & Buzz for numbers from 1 to 100.
* If number is multiples of '3' print "Fizz".
* If number is multiples of '5' print "Buzz".
* If number is multiples of '3' & '5' print "FizzBuzz" instead of the number.
*/
public class FizzBuzzJava {
public static void main(String[] args) {
int n = 100;
// loop for 100 times
for (int i=1; i<=n; i++)
{
// number divisible by 3 & 5, print 'FizzBuzz'
if (i%15==0) {
System.out.print("FizzBuzz"+" ");
}
// number divisible by 5, print 'Buzz'
else if (i%5==0) {
System.out.print("Buzz"+" ");
}
// number divisible by 3, print 'Fizz'
else if (i%3==0) {
System.out.print("Fizz"+" ");
}
// print the numbers
else {
System.out.print(i+" ");
}
}
}
}