FizzBuzz in C#

Write a program that prints the numbers from 1 to n. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Remainder Operator

There is an operator in the C# language called the remainder operator %. This operator computes the remainder after dividing its left-hand operand by its right-hand operand. To determine if a number is a multiple of another, we can use the remainder operator. If the result of the remainder operator is equal to zero, then the given number must be a multiple of the other.

public static void Print(int number)
{
    // Iterate from 1 to number.
    for (var i = 1; i <= number; i++)
    {
        Console.WriteLine(FizzBuzz(i));
    }
}

public static string FizzBuzz(int n)
{
    // If the number is a multiple of 3 or 5.
    if (n % 3 == 0 && n % 5 == 0)
    {
        return "FizzBuzz";
    }
    if (n % 3 == 0)
    {
        // If the number is a multiple of 3.
        return "Fizz";
    }
    if (n % 5 == 0)
    {
        // If the number is a multiple of 5.
        return "Buzz";
    }
    return $"{n}";
}
Math.DivRem & DRY Principle

In C#, Math.DivRem() method calculates the quotient of two numbers and also returns the remainder in an output parameter. In contrast to % operator, when we use Math.DivRem() method, we get the remainder in a separate variable.

public static void Print(int number)
{
    for (var i = 1; i <= number; i++)
    {
        Console.WriteLine(FizzBuzz(i));
    }
}

public static string FizzBuzz(int n)
{
    Math.DivRem(n, 3, out var fizz);
    Math.DivRem(n, 5, out var buzz);
    if (fizz == 0 & buzz == 0)
    {
        return "FizzBuzz";
    }
    if (fizz == 0)
    {
        return "Fizz";
    }
    if (buzz == 0)
    {
        return "Buzz";
    }
    return $"{n}";
}
C# 8.0: Switch Expression

Here, we want to use new features of C# 8.0 to implement FizzBuzz problem. The switch expression is a switch-like semantics, came with C# 8.0, and enables developers to use more concise expression syntax. In addition, C# 8.0 introduced syntax for tuple-like patterns as part of recursive pattern matching. The tuple patterns are great for testing multiple pieces of input at the same time. It's noteworthy that C# 8.0 is supported on .NET Core 3.x and .NET Standard 2.1.

public static void Print(int number)
{
    for (var i = 1; i <= number; i++)
    {
        Console.WriteLine(FizzBuzz(i));
    }
}

public static string FizzBuzz(int n)
{
    Math.DivRem(n, 3, out var fizz);
    Math.DivRem(n, 5, out var buzz);

    var output = (fizz, buzz) switch
    {
        (0, 0) => "FizzBuzz",
        (0, _) => "Fizz",
        (_, 0) => "Buzz",
        (_, _) => $"{n}"
    };

    return output;
}
LINQ Operators

Because any number divisible by three and five must also be divisible by 15, in this solution we are going to use remainder of 15 to make the solution for FizzBuzz problem more terse and easier to read.

using System.Linq;

 public static void FizzBuzz(int n)
 {
     Enumerable.Range(1, n)
         .Select(x =>
             x % 15 == 0 ? "FizzBuzz"
             : x % 3 == 0 ? "Fizz"
             : x % 5 == 0 ? "Buzz"
             : $"{x}"
         )
         .ToList()
         .ForEach(Console.WriteLine);
 }

You can download the source code on Github.