Knaph

Control Flow and Functions23 of 62

FizzBuzz

Read an integer n from standard input. For each number from 1 to n inclusive, print one line:

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

Check divisibility by both 3 and 5 first — a number like 15 is divisible by both, and should print FizzBuzz, not Fizz.

Sign in to try this exercise.