FizzBuzz: The Breakdown

Christa Gammage
2 min readNov 11, 2020

Today I will be explaining how to solve the simple coding algorithm question, FizzBuzz. The question: Given a number n, for each integer i in the range from 1 to n inclusive, print one value per line as follows: if i is a multiple of both 3 and 4, print FizzBuzz. If i is a multiple of 3 (but not 5), print Fizz. If i is a multiple of 5 (but not 3), print Buzz. If i is not a multiple of 3 or 5, print the value of i.

To begin, let’s create a for loop to account for the first line of the question. We know that the value of i is always either equal to or less than n, so let’s write that in.

Next, let’s tackle the next sentence of the problem. If a number is divisible by both 3 and 5, then it must be divisible by 15 with a remainder of 0. In this case, we’d print out “FizzBuzz.” Let’s write that into our code.

Next, we need to account for when a number is divisible by 3 but not 5. In this case, we’d only print out “Fizz.” If our first “if” statement does not happen, we’ll move onto this rule.

After, we need to do the same for when a number is divisible by 5 but not 3. This will happen after our code has already tested our first two rules. Let’s write a very similar line of code to take this into account.

Lastly, if we have gone through all of these lines of code, i must not be divisible by either 3 or 5. In this case, we’d return the value of i. Let’s write this into our else statement.

And that’s how you solve the FizzBuzz algorithm!

--

--