Solving the classic FizzBuzz problem with C# .NET

In this post we’ll go through a classic interview question for developers: the FizzBuzz problem.

You are asked to write a function that accepts an integer. The function must implement a loop from 1 to the integer that was passed into the function. In the loop the function must print the following to the command window:

  • if the number is divisible by 3 then print “fizz”
  • if the number is divisible by 5 then print “buzz”
  • if the number is divisible by both 5 and 3 then print “fizzbuzz”
  • otherwise just print the number itself

We apply only one of these rules in the loop. E.g. 15 is divisible by both so we only print “fizzbuzz”, not “fizz”, then “buzz” and finally “fizzbuzz”.

The problem is simple to solve. However, if you have never encountered it before then it might turn into a tricky one in a stressful interview situation, especially with someone watching what you’re doing. So make sure you remember the below guidelines so that you’ll have more time over for the more complex tasks.

The solution is based on the relatively rarely used % operator, called the modulus. It returns the remainder of a division: e.g. 5 % 2 = 1 since we can fit 2 twice into 5 and we have a remainder of 1. Likewise 15 % 3 = 0 because we can fit 3 into 15 five times with nothing remaining.

We’ll have a couple of if-statements in the loop to check for the modulus. There are couple of details to watch out for:

  • check first if the integer is divisible by 3 and 5
  • use else-if-statements for the other conditions so that only one conditional block is carried out, i.e. the first one that meets the condition(s)

Why check for divisibility by 3 and 5 before anything else? Say that we get to 15 in the loop. If our first condition is to only check for divisibility by 3 then we’ll print “fizz” and that’s not what we want.

Why use else-if statements? If we have 3 “normal” if-statements then all of them will be evaluated and we don’t want that either. Again, taking 15 as an example, it is divisible by 3 which will print “fizz”, it is also divisible by 5 which will print “buzz” and then we also print “fizzbuzz” since 15 also fulfils the third condition.

Here’s the solution in C# code, but it can easily be ported to other popular OOP languages:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms
    public class FizzBuzz
        public void RunFizzBuzz(int until)
            for (int i = 1; i <= until; i++)
                if (i % 3 == 0 && i % 5 == 0)
                else if (i % 3 == 0)
                else if (i % 5 == 0)

…and here’s a unit test to see if it works:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Algorithms;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Algorithms.Tests
    public class FizzBuzzTests
        public void RunFizzBuzzTests()
            FizzBuzz fb = new FizzBuzz();
            StringWriter output = new StringWriter();
            string console = output.ToString();
            Assert.AreEqual(string.Format("1{0}2{0}fizz{0}4{0}buzz{0}fizz{0}7{0}8{0}fizz{0}buzz{0}11{0}fizz{0}13{0}14{0}fizzbuzz{0}16{0}17{0}fizz{0}19{0}buzz{0}", Environment.NewLine), console);

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

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

You are commenting using your 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

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: