Here's the correct implementation:
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
We check "both" first, then "only ", then "only ", then neither. This ordering ensures each number gets exactly the right output.
Alternative: check i % 15 == 0 for FizzBuzz, since is the LCM of and .