The FizzBuzz Test

I was stunned when I read that 199 out of 200 programmers can’t program — and I’m not speaking figuratively — they can’t write a single line of code.

This fact intrigued me, so I Google for more information on it. That is when I ran into the FizzBuzz Test. Basically, the test is as follows;

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Here’s mine, in PHP;

FizzBuzz();

function FizzBuzz()
{
for ($i = 1; $i <= 100; $i++)
{
$out = (($i % 3 == 0) && ($i % 5 == 0)) ? “FizzBuzz” : (($i % 3 == 0) ? “Fizz” : (($i % 5 == 0) ? “Buzz” : $i));

if ($i == 1)
{
echo ‘[a href=”http://kylestubbins.com/node” alt=”Return to KyleStubbins.com home” title=”Return to KyleStubbins.com home”]KyleStubbins.com[/a][br /][br /]';
echo ‘[b]Fizz[/b] is divisible by 3[br /][b]Buzz[/b] is divisible by 5[br /][b]FizzBuzz[/b] is divisible by 3 AND 5[br /][br /]';
echo “Let’s Begin![br /][br /]”;
}

echo $out.'[br /]';
}
echo ‘[br /][br /]All Done![br /][br /]';
echo ‘[a href=”http://kylestubbins.com/node” alt=”Return to KyleStubbins.com home” title=”Return to KyleStubbins.com home”]KyleStubbins.com[/a][br /][br /]';
}

Note: I have changed all < > brackets to [ ] bbcode style simply so they wouldn’t parse in this post! Sorry, but the formatting won’t hold for the post either! This really could be cut down to a couple of lines — all of the “extra” stuff is to display the start and end, and to add a link to get you back here, but is not needed for the actual “test”.

You can see the results here. Feel free to post yours! :)

One thought on “The FizzBuzz Test”

  1. #include

    int main(int argc, char **argv)
    {
    printf(“hello world\n”);
    for(int i=1; i<101;i++)
    {
    printf(“%03d: “,i);
    if(i%3==0)
    printf(“Fizz”);
    if(i%5==0)
    printf(“Buzz”);
    //if(i%3==0 || i%5==0) if we’re not printing out each line
    printf(“\n”);
    }

    return 0;
    }

Leave a Reply