FizzBuzz
Print the numbers 1 through 20. Replace multiples of 3 with Fizz, multiples of 5 with Buzz, and multiples of both with FizzBuzz.
Course progress
Problem 1 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
Numbers 1 through 20
Expected output
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz
What is happening?
Each number is tested for divisibility. For example, 3 becomes Fizz, 5 becomes Buzz, and 15 becomes FizzBuzz because it is divisible by both 3 and 5.
Solution strategy
Build the right mental model
Check divisibility by 15 first so a number divisible by both 3 and 5 is not handled by an earlier branch.
Core concepts
loops · conditions
Complexity
Time O(n), space O(1).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for fizzbuzz
using System;
class Program
{
static void Main()
{
for (int number = 1; number <= 20; number++)
{
if (number % 15 == 0) Console.WriteLine("FizzBuzz");
else if (number % 3 == 0) Console.WriteLine("Fizz");
else if (number % 5 == 0) Console.WriteLine("Buzz");
else Console.WriteLine(number);
}
}
}Knowledge check
Questions students often ask
What is the main idea behind fizzbuzz?
Check divisibility by 15 first so a number divisible by both 3 and 5 is not handled by an earlier branch.
What is the time and space complexity of this solution?
Time O(n), space O(1).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.