How to solve Fizz Buzz Problem in C#

In this article, we will learn here a simple Program where Prints from 1 to 200 and for multiples of  2, it displays "Fizz" and for multiples of 7, it displays Buzz. For numbers which are divided by both 2 and 7, it displays Fizz Buzz.


using System;

using System.Collections.Generic;

using System.Text;



namespace testconsole

{

 class Program

 {

  static void Main(string[] args)

  {



   int totalnumber = 200;

   int fizz = 2; //2 for fiz 

   int buzz = 7; // 7 for Buz 

   for (int i = 1; i <= totalnumber; i++)

   {

    if (i % fizz == 0 && i % buzz == 0) // display FizBuz If  number is divide by 2 and 7

    {

     Console.WriteLine("fizz Buzz");

    } else if (i % fizz == 0 && i % buzz != 0) // Diplay fiz if number divided by 2

    {

     Console.WriteLine("fizz");

    } else if (i % buzz == 0 && i % fizz != 0) // Diplay Buzz if number divided by 7

    {

     Console.WriteLine("Buzz");

    } else // else display number inseated of Fiz/Buzz/FizBuzz

    {

     Console.WriteLine(i);



    }

   }



   Console.ReadLine();



  }

 }

}




Comments

Popular posts from this blog

Bootstrap Modal Popup keep open on PostBack in ASP.Net

Resolved Issue in Asp core 3.0 serializersettings does not exist in AddJsonOptions

.Net most asked interview questions for experienced professionals (C#,Asp WEBFORM,MVC,ASP CORE,WEB API,SQL Server,Java Script,Jquery)