Maintained by Vikas Dulgunde, software engineer
You are given a single positive integer n. Walk through the whole numbers from 1 up to and including n and decide what each one becomes in the output.
The rule depends on divisibility. A number divisible by 3 turns into "Fizz", a number divisible by 5 turns into "Buzz", and a number divisible by both 3 and 5 turns into "FizzBuzz". Any number that is divisible by neither stays as its own value, written as a string.
Return the results in order, so position i of the output corresponds to the number i (counting from 1). The output always has exactly n entries, and every entry is a string.
1 and 2 are plain numbers, 3 is a multiple of 3 so it becomes Fizz, 4 is plain, and 5 is a multiple of 5 so it becomes Buzz.
15 is the first number divisible by both 3 and 5, so it becomes FizzBuzz. Every third slot is Fizz and every fifth slot is Buzz, except where they overlap.
1 is divisible by neither 3 nor 5, so it stays as the string "1".
Visible test cases
Loop from 1 to n and handle one number at a time. The hard part is only choosing which of the four outputs each number gets.
Order your checks carefully. Test divisibility by both 3 and 5 first, otherwise a multiple of 15 would match the Fizz branch and never reach FizzBuzz.
A number is divisible by both 3 and 5 exactly when it is divisible by 15, so a single i % 15 check captures the overlap.
Iterate once through 1..n and pick the output for each value using modulo tests, checking the combined 3-and-5 case before the single cases.
Build each entry by appending "Fizz" when divisible by 3 and "Buzz" when divisible by 5, which produces "FizzBuzz" automatically for multiples of both and avoids a separate combined test.