Caveman's Blog

My commitment to learning.

Drinking game – Fizz Buzz

leave a comment »


“Fizz Buzz” also called as “Bizz Buzz” is a drinking game. Players generally sit in a circle. The player designated to go first says the number “1”, and each player thenceforth counts one number in turn. However, any number divisible by three is replaced by the word bizz and any divisible by five by the word buzz. Numbers divisible by both become bizz buzz. A player who hesitates or makes a mistake is either eliminated or must pay a forfeit, such as taking a drink. [1]

Fizz Buzz is also used as a fun coding exercise like this: print all numbers from 1 – 100, while:

1. replacing the numbers divisible by 3 with the word “Fizz”.
2. replacing the number divisible by 5 with the word “Buzz”.
3. replacing the number that is divisible by 3 and 5 with the word “FizzBuzz”.

Following are four C# implementations of this problem:

            for (int i = 1; i <= 100; i++)
            {
                Console.WriteLine((i % 3 == 0 && i % 5 == 0) ? "FizzBuzz" :
                    ((i % 3 == 0) ? "Fizz" : (i % 5 == 0) ? "Buzz" : i.ToString()));
            }
            string s = "";
            for (int i = 1; i <= 100; i++)
            {
                if (i % 3 == 0)
                    s = "Fizz";
                if (i % 5 == 0)
                    s += "Buzz";

                if (i % 3 != 0 && i % 5 != 0)
                    Console.WriteLine(i);
                else
                    Console.WriteLine(s);
            for (int i = 1; i <= 100; i++)
            {
                if (i % 15 == 0)
                    Console.WriteLine("FizzBuzz");
                else if (i % 3 == 0)
                    Console.WriteLine("Fizz");
                else if (i % 5 == 0)
                    Console.WriteLine("Buzz");
                else
                    Console.WriteLine(i);
            }
            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);
            }

References:
1. Bizz Buzz – Wikipedia

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: