-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFizzBuzz.java
More file actions
36 lines (30 loc) · 756 Bytes
/
FizzBuzz.java
File metadata and controls
36 lines (30 loc) · 756 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
34
35
36
public class FizzBuzz {
public static void main(String[] args) {
//1. factors of 3 and 5 - print FizzBuzz
//2. factors of 3 print Fizz
//3. factors of 5 print Buzz
int[] array = {30, 5, 3 ,15, 9, 6 ,10, 20};
for(int i : array) {
if( i % 3 == 0 && i % 5 == 0)
System.out.print("FizzBuzz");
else if(i % 3 == 0)
System.out.print("Fizz");
else if(i % 5 == 0)
System.out.print("Buzz");
else
System.out.print("");
}
System.out.println();
StringBuilder s = new StringBuilder();
for(int i: array) {
//1. If divisible by 3 - append Fizz
if(i % 3 == 0)
s.append("Fizz");
//2. If divisible by 5 - append Buzz
if(i % 5 == 0)
s.append("Buzz");
//3 If not, append empty
}
System.out.println(s.toString());
}
}