-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFizzBuzz.java
More file actions
36 lines (35 loc) · 758 Bytes
/
FizzBuzz.java
File metadata and controls
36 lines (35 loc) · 758 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
/**
* @author Kshitij Grover
*/
class FizzBuzz {
public static void main(String[] args) {
//use a static method as it's not attached to an Object
executeFizzBuzz(100);
}
public static void executeFizzBuzz(int n){
/*
* Loop from one to the parameter, which
* is the desired max.
*/
for(int i = 1; i < n; i++){
String result = "";
// print "FizzBuzz" if divisible by 5 and 3
if(i % 15 == 0) {
result = "FizzBuzz";
}
//else if only by 3, "Fizz" is printed
else if(i % 3 == 0) {
result = "Fizz";
}
//else if only by 5, "Buzz" is printed
else if(i % 5 == 0) {
result = "Buzz";
}
//otherwise, print the number itself
else{
result = "" + i;
}
System.out.println(result);
}
}
}