“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.

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

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

Hope this will help you.
Thanks.


ricky nijlandPosted May 28, 2020, 7:13 AM EDT
Int[] words = { 3, 5, 7, 9, 11 }; string[] whatWord = { "Fizz ", "Buzz ", "is ", "weird ", "game"}; for (int i = 1; i <= 100; i++) { string output = ""; for(int x = 0; x < words.Length; x++) { if (i % words[x] == 0) output += whatWord[x]; } if (output == "") output = Convert.ToString(i); Console.WriteLine(output);
Georgi MatinskiPosted Sep 24, 2019, 3:05 AM EDT
In my code it writes the 3 and the fizz
Serge KlokovPosted Jul 25, 2019, 7:41 PM EDT
See my example here: https://github.com/sergeklokov/FizzBuzz/blob/master/Program.cs
Serge KlokovPosted Jul 25, 2019, 7:40 PM EDT
Anoop, you can change line #3 in your code to if (i % 15 == 0 )