The FizzBuzz Problem

FizzBuzz is a classic programming exercise that's often used in job interviews to test basic programming skills and logical thinking.

The Problem

Write a program that prints the numbers from 1 to 100, but:

  • For multiples of 3, print "Fizz" instead of the number
  • For multiples of 5, print "Buzz" instead of the number
  • For multiples of both 3 and 5, print "FizzBuzz" instead of the number

A Simple Solution

Here's a straightforward implementation in C#:

for (int i = 1; i <= 100; i++)
{
    if (i % 15 == 0)
        Console.WriteLine("FizzBuzz");
    else if (i % 3 == 0)
        Console.WriteLine("Fizz");
    else if (i % 5 == 0)
        Console.WriteLine("Buzz");
    else
        Console.WriteLine(i);
}

Why This Works

The key insight is checking for divisibility by 15 first (since 15 = 3 × 5), then checking for divisibility by 3 and 5 individually. The modulo operator (%) returns the remainder after division, so if it equals 0, the number is divisible.

Expected Output

The first 20 lines of output would be:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz

This simple exercise tests understanding of loops, conditionals, and the modulo operator. While it may seem trivial, it reveals a candidate's ability to think logically and write clean, readable code.