Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 59 / 68

FizzBuzz (the classic exercise)

This lesson covers solving FizzBuzz in R, with the aim of combining branching and looping.

FizzBuzz is the classic programming exercise. Print the numbers from 1 upwards, but print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both. It is perfect practice for branching.

The sample code loops from 1 to 15 and checks i %% 15 == 0 first so that multiples of 15 take priority, then multiples of 3 and 5. %% is R's remainder operator.

A common early stumble is the order of the checks. Test 3 and 5 first and a multiple of 15 prints just "Fizz", so the strictest condition has to come first.

Simple as it looks, getting the order wrong breaks it — which is why it turns up so often in interviews and training as a check on the fundamentals.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run R directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)