-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathFizzBuzz.java
More file actions
29 lines (25 loc) · 797 Bytes
/
FizzBuzz.java
File metadata and controls
29 lines (25 loc) · 797 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
import java.util.ArrayList;
import java.util.Scanner;
public class FizzBuzz{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number - ");
int n = sc.nextInt();
List<String> result = fizzBuzz(n);
System.out.println("Resultant String Array" + result);
}
private static List<String> fizzBuzz(int n) {
List<String> list = new ArrayList<String>();
for (int i=1; i<=n; i++){
if ((i%3 == 0) && (i%5 == 0))
list.add("FizzBuzz");
else if (i%3 == 0)
list.add("Fizz");
else if (i%5 == 0)
list.add("Buzz");
else
list.add(String.valueOf(i));
}
return list;
}
}