Search code examples
c#switch-statementfizzbuzz

C# FizzBuzz Switch Solution


Is it possible to create the FizzBuzz solution in C# with the switch construct. I've found solutions that work for JavaScript and other languages, but these (or the syntax equivalent) don't seem to be working in C#.

For reference, I'll write the if statement version down below:

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

EDIT: forgot to put my switch statement code, sorry about that:

for (x = 1; x <= 15; x++)
{
    switch (x)
    {
        case (x % 3 == 0 && x % 5 == 0):
            Console.WriteLine("FizzBuzz");
            break;
        case (x % 5 == 0):
            Console.WriteLine("Buzz");
            break;
        case (x % 3 == 0):
            Console.WriteLine("Fizz");
            break;
        default:
            Console.WriteLine(x);
            break;
    }
}

My problem is with the modulo statements. Error is "Cannot implicitly convert type bool to int. I've tried replacing switch (x) with switch (true) but that doesn't help much, just changes the error to "A constant value is expected" for each of my cases.


Solution

  • "Is it possible to create the FizzBuzz solution in C# with the switch construct"

    Yes, it is possible, but not very practical (compared to an if statement). This is because you switch on an expression, and then your case statements must compare a constant expression to that value. So you can't do switch (0) and then case (i % 15) because i % 15 is not a constant expression.

    Given this, you can switch on i % 15 to reduce the number of comparisons to the known base set, and then special-case the results that are divisible by 3, 5, and 15:

    for (int x = 1; x <= 15; x++)
    {
        switch (x % 15)
        {
            // Evenly divisible by 15
            case 0:
                Console.WriteLine("FizzBuzz");
                break;
    
            // Evenly divisible by 3
            case 3:
            case 6:
            case 9:
            case 12:
                Console.WriteLine("Fizz");
                break;
    
            // Evenly divisible by 5
            case 5:
            case 10:
                Console.WriteLine("Buzz");
                break;
    
            // Everything else
            default:
                Console.WriteLine(x);
                break;
        }
    }
    

    But the if statement is much more concise:

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