Math Fundamentals18 sections · 814 units
Open in Course

Fizz Buzz - Implementation

In pseudocode

Here's the solution:


function fizzBuzz(n)
    result := empty array
    for i from 1 to n
        if i mod 15 = 0 then
            append "FizzBuzz" to result
        else if i mod 3 = 0 then
            append "Fizz" to result
        else if i mod 5 = 0 then
            append "Buzz" to result
        else
            append string representation of i to result
    return result

Notice imod15=0i \bmod 15 = 0 is the same as checking both imod3=0i \bmod 3 = 0 and imod5=0i \bmod 5 = 0, because 15=3×515 = 3 \times 5. This simplifies the condition.