“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. I am using visual Studio 2022.
Create New project in Visual Studio 2022.My solution is look like as below screen.
My solution and project name is "Practice".
Add new class with Method
public static void fizzBuzz(int n)
{
for (int i = 1; i <= n; 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);
}
}
}
Now it is final time to put code in the Program.cs
using Practice;
Console.WriteLine("Hello, World!");
Console.WriteLine("Please enter your number ");
int n = Convert.ToInt32(Console.ReadLine());
Result.fizzBuzz(n);
Output are as below
Thank you Hope it is help you.
No comments:
Post a Comment