Home  >  Article  >  Backend Development  >  Write a C# program to solve the FizzBuzz problem

Write a C# program to solve the FizzBuzz problem

WBOY
WBOYforward
2023-08-25 20:01:101290browse

编写一个 C# 程序来解决 FizzBu​​zz 问题

FizzBuzz The question states -

  • displays "Fizz" instead of the number for each multiple of 3,
  • displays " Buzz" instead of every multiple of 5.
  • Display "FizzBuzz" instead of numbers for each multiple of 5 and 3

Let's see how to implement the above using C# -

Example

using System;

class Demo {

   static void Main(String[] args) {
      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);
      }
   }
}

The above is the detailed content of Write a C# program to solve the FizzBuzz problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete