FizzBuzz

By mOrPhie on Wednesday 20 June 2007 15:41
Categories: life, software engineering, techrelated, Views: 526

FizzBuzz is a common example to test whether someone is competent in programming. The assignment is to write a program which prints 1 to 100. For every multiple of 3 print ‘Fizz’ and for every multiple of 5 print ‘Buzz’. For every multiple of 3 _and_ 5 print ‘FizzBuzz’.

The trick is that most of the time only competent programmers know of the “modulo” operator (% in C, C++, C# and others). Silly thing is: it’s usually true! Why? No one knows…

Competent programmers should be able to com up with this under 5 minutes:


C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for (int i = 1i <= 100i++)
{
    if (i % 3 == 0 && i % 5 == 0)
    {
        Console.WriteLine("FizBuz");
    }
    else if (i % 3 == 0// && i % 5 != 0
    {
        Console.WriteLine("Fiz");
    }
    else if (i % 5 == 0// && i % 3 != 0
    {
        Console.WriteLine("Buz");
    }
    else
    {
        Console.WriteLine(i);
    }
}


Now don’t rely on this example. There are other tests out there. Still, it’s quite funny to see your colleagues struggle with something so simple and finally bang their head against the table if you tell them the trick.

Volgende: Microsoft DevDays 2007 06-'07

Comments


By T.net user wkleunen, Tuesday 08 April 2008 12:03

You think it is "funny" that your colleagues don't know this "trick"? I would say it is painfull.

Comment form
(required)
(required, but will not be displayed)
(optional)

Please enter the code from the image below: