C# FizzBuzz

Here is my C# and .NET Core FizzBuzz program.

Prerequisites

The versions of the following items that I used are displayed in brackets.

  1. Install Visual Studio Code. (1.34)
  2. Install the .NET Core SDK. (2.2.300)
  3. Install the C# extension for Visual Studio Code (1.19.1)

FizzBuzz

Start Visual Studio Code, open the Integrated Terminal (CTRL + or View > Integrated Terminal) and create a new console app by typing:

dotnet new console

Add the code below to the Program.cs file

using System;

namespace CSharpFizzBuzz
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                bool fizz = i % 3 == 0;
                bool buzz = i % 5 == 0;

                if (fizz == true &amp;&amp; buzz == true)
                {
                    Console.WriteLine("FizzBuzz");
                }
                else if (fizz == true)
                {
                    Console.WriteLine("Fizz");
                }
                else if (buzz == true)
                {
                    Console.WriteLine("Buzz");
                }
                else
                {
                    Console.WriteLine(i);
                }

            }
            Console.Read();
        }
    }
}

Run the program

dotnet run

Inspect the output.

The completed example can be found on GitHub

Acknowledgements

Getting Started with C# and Visual Studio Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.