FizzBuzz In C#

“FizzBuzz” is an interview question asked during interviews to check logical skills of developers.

For Demonstration, we will print number starting from 1 to 100. When a number is multiple of three, print “Fizz” instead of a number on the console and if multiple of five then print “Buzz” on the console. For numbers which are multiple of three as well five, print “FizzBuzz” on the console.

 FizzBuzz in C#

Let’s try (There are several methods to create a FizzBuzz program),

Method 1

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

Preview

FizzBuzz in C#

Method 2

for (int i = 1; i <= 100; i++)  
{  
    string str = "";  
    if (i % 3 == 0)  
    {  
        str += "Fizz";  
    }  
    if (i % 5 == 0)  
    {  
        str += "Buzz";  
    }  
    if (str.Length == 0)  
    {  
        str = i.ToString();  
    }  
    Console.WriteLine(str);  
  
}

Preview

FizzBuzz in C#

Hope this will help you.

Thanks.