-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFizzBuzz.java
More file actions
30 lines (25 loc) · 798 Bytes
/
FizzBuzz.java
File metadata and controls
30 lines (25 loc) · 798 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
import java.util.Scanner;
public class FizzBuzz {
public static void fizzbuzz(int n) {
for (int i = 1; i <= n; ++i) {
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(i + " ");
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get input for n
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
// Call the fizzbuzz function with the given n
fizzbuzz(n);
scanner.close();
}
}