-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFizzBuzz.java
More file actions
36 lines (28 loc) · 928 Bytes
/
Copy pathFizzBuzz.java
File metadata and controls
36 lines (28 loc) · 928 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
package LeetcodePractice;
import java.util.ArrayList;
import java.util.List;
public class FizzBuzz {
public static List<String> fizzBuzz(int n) {
List<String> result = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (i % 15 == 0) {
result.add("FizzBuzz");
} else if (i % 3 == 0) {
result.add("Fizz");
} else if (i % 5 == 0) {
result.add("Buzz");
} else {
result.add(Integer.toString(i));
}
}
return result;
}
public static void main(String[] args) {
int n1 = 3;
int n2 = 5;
int n3 = 15;
System.out.println("Input: " + n1 + " -> Output: " + fizzBuzz(n1));
System.out.println("Input: " + n2 + " -> Output: " + fizzBuzz(n2));
System.out.println("Input: " + n3 + " -> Output: " + fizzBuzz(n3));
}
}