FizzBuzz
The task
Problem
Print the numbers 1 through 20. Replace multiples of 3 with Fizz, multiples of 5 with Buzz, and multiples of both with FizzBuzz.
How to think about it
Approach
Check divisibility by 15 first so a number divisible by both 3 and 5 is not handled by an earlier branch.
Complete C# solution
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);
}
}
}Verified output
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz