Educatifu
Open menu

FizzBuzz

Easymathsimulation

The classic. For each number from 1 to n, print one line:

  • Fizz if the number is divisible by 3,
  • Buzz if it is divisible by 5,
  • FizzBuzz if it is divisible by both 3 and 5,
  • otherwise the number itself.

Input

A single integer n.

Output

n lines as described above, in order from 1 to n.

Examples

Example 1

Input
5
Output
1
2
Fizz
4
Buzz

1, 2, Fizz (3), 4, Buzz (5).

Example 2

Input
15
Output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

15 is divisible by both 3 and 5, so the last line is FizzBuzz.

Example 3

Input
3
Output
1
2
Fizz

Constraints

  • 1 <= n <= 10^4

Hints

Reveal hint 1

Check divisibility by 15 (i.e. by both 3 and 5) FIRST, otherwise you print Fizz or Buzz before you ever reach FizzBuzz.

Reveal hint 2

The modulo operator `%` gives the remainder: a number is divisible by 3 when `i % 3 == 0`.

Reference solution (spoiler — try it yourself first)

Approach

Loop from 1 to n. Test divisibility by 15 first (both 3 and 5), then 3, then 5, otherwise print the number.

n = int(input())
for i in range(1, n + 1):
    if i % 15 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

← All practice problems

Bring us the problem, not a perfect specification

Tell us what needs to change, who it affects and any important deadline. We will review the context and reply with useful next questions.

  1. 01Share contextDescribe the workflow, constraint or risk.
  2. 02Clarify togetherWe identify missing facts and useful options.
  3. 03Choose a startAgree a focused assessment or delivery step.
Start a conversation