Java bootcamp · Lab 13

FizzBuzz

mediumFor12 minLesson: For Loop

Read the question, write Java on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read n. For i=1..n print FizzBuzz / Fizz / Buzz / i (classic rules), one per line.

Examples

Example 1
Input
5
Output
1
2
Fizz
4
Buzz
Hint
  1. Check % 15 before % 3 and % 5.
Show correct code

Peek only after you have tried. You can still Check your own version.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    for (int i = 1; i <= n; i++) {
      if (i % 15 == 0) System.out.println("FizzBuzz");
      else if (i % 3 == 0) System.out.println("Fizz");
      else if (i % 5 == 0) System.out.println("Buzz");
      else System.out.println(i);
    }
  }
}
Main.javaJava 21 · javac · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.