C# program to solve FizzBuzz problem
The 'FizzBuzz' problem is a classic programming exercise where you need to print numbers from 1 to a given limit, replacing multiples of 3 with "Fizz," multiples of 5 with "Buzz," and multiples of both 3 and 5 with "FizzBuzz." Here's a C# program that solves the FizzBuzz problem:
using System;
class FizzBuzz
{
static void Main()
{
Console.Write("Enter the limit: ");
int limit = int.Parse(Console.ReadLine());
for (int i = 1; i <= limit; 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);
}
}
}
}
In this program, the 'Main' method prompts the user to enter the limit, which determines the range of numbers to be printed. It uses a 'for' loop to iterate from 1 to the specified limit.
Inside the loop, it checks if the current number is divisible by both 3 and 5 first, using the condition 'i % 3 == 0 && i % 5 == 0'. If it is, "FizzBuzz" is printed. Next, it checks if the current number is divisible by 3 using the condition 'i % 3 == 0' and prints "Fizz" in that case. Similarly, if the number is divisible by 5 ('i % 5 == 0'), "Buzz" is printed. If none of the conditions are met, the program simply prints the current number.
To use this program, you can copy and paste it into a C# development environment, such as Visual Studio, and run it. The program will ask for the limit, and then it will print the numbers according to the FizzBuzz rules.