Solve Fizz Buzz problem using case when statement - CSharp Language Basics

CSharp examples for Language Basics:switch

Description

Solve Fizz Buzz problem using case when statement

Demo Code

using System;// w w w.  j  a v a 2  s  .  c om
using System.Collections.Generic;
using System.Text;
class FizzBuzz
{
   static void Main()
   {
      for (int i = 1; i < 50; i++)
      {
         PrintFizzBuzz(i);
      }
   }
   static void PrintFizzBuzz(int n)
   {
      switch (n)
      {
         case var _ when n % 3 == 0:
            Console.Write("Fizz");
            goto case 0;
         case 0:
            break;
         case var _ when n % 5 == 0:
            Console.Write("Buzz");
            break;
         default:
            Console.WriteLine(n);
            break;
      }
   }
}

Result


Related Tutorials