FizzBuzz
Read n. For each number i from 1 to n, DISPLAY "FizzBuzz" if i is a multiple of 15, "Fizz" if it is a multiple of 3, "Buzz" if it is a multiple of 5, and i otherwise.
| Input (one value per INPUT ()) | Expected output |
|---|---|
| 5 | 1 2 Fizz 4 Buzz |
| 15 | 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz |
Check runs all 3 tests. Press Run to try the program yourself: a box asks for each INPUT ().
Output
Variables
Robot grid The triangle is the robot, the star is the goal, dark squares are walls.
Check
| Test | Input | Expected | Your output |
|---|
Show a solution
n ← INPUT () i ← 1 REPEAT n TIMES { IF (i MOD 15 = 0) { DISPLAY ("FizzBuzz") } ELSE { IF (i MOD 3 = 0) { DISPLAY ("Fizz") } ELSE { IF (i MOD 5 = 0) { DISPLAY ("Buzz") } ELSE { DISPLAY (i) } } } i ← i + 1 }
Teachers: sign in as a teacher to add this exercise to a class, graded automatically.
Frequently asked questions
How do I solve this exercise?
Complete the starter code in the editor, press Run to try it (type values when INPUT () asks), then press Check to run every test. Step shows the program line by line with its variables.
Where is the solution?
Under the editor, in Show a solution. Try the exercise first: comparing your code with the solution afterwards is a good way to learn another approach.
AP is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site.