[ create a new paste ] login | about

Recent implementations of FizzBuzz, in:
[ C | C++ | PHP | Perl | Python | Ruby ]

C:
pasted on Sep 28:
/*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”.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>

int main(int argc, char* argv[])
{
    int i;
    for (i = 1; i <= 100; i++)
    {
#if 0
        int val = 0;
        if ((i % 3) == 0) val |= 1; // fizz
        if ((i % 5) == 0) val |= 2; // buzz
        switch( val )
        {
        default: printf( "%d\n", i ); break;
        case 1: printf( "Fizz\n" ); break;
        case 2: printf( "Buzz\n" ); break;
        case 3: printf( "FizzBuzz\n" ); break;
        }
#else
        bool fizz = (i % 3) == 0; // fizz
        bool buzz = (i % 5) == 0; // buzz
        if (fizz && buzz)
        {
            printf( "FizzBuzz\n" );
        }
        else if (fizz)
        {
            printf( "Fizz\n" );
        }
        else if (buzz)
        {
            printf( "Buzz\n" );
        }
        else
        {
            printf( "%d\n", i );
        }       
#endif
    }

    return 0;
}
view (49 lines, 100 lines of output)
pasted on Aug 15:
/*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”.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>

int main(int argc, char* argv[])
{
    int i;
    for (i = 1; i <= 100; i++)
    {
#if 0
        int val = 0;
        if ((i % 3) == 0) val |= 1; // fizz
        if ((i % 5) == 0) val |= 2; // buzz
        switch( val )
        {
        default: printf( "%d\n", i ); break;
        case 1: printf( "Fizz\n" ); break;
        case 2: printf( "Buzz\n" ); break;
        case 3: printf( "FizzBuzz\n" ); break;
        }
#else
        bool fizz = (i % 3) == 0; // fizz
        bool buzz = (i % 5) == 0; // buzz
        if (fizz && buzz)
        {
            printf( "FizzBuzz\n" );
        }
        else if (fizz)
        {
            printf( "Fizz\n" );
        }
        else if (buzz)
        {
            printf( "Buzz\n" );
        }
        else
        {
            printf( "%d\n", i );
        }       
#endif
    }

    return 0;
}
view (49 lines, 100 lines of output)


C++:
pasted on Oct 11:
#include <iostream>

// Prints numbers "1" through "100" as integers.
// Every multiple of "3" is printed as "Fizz".
// Every multiple of "5" is printed as "Buzz".
// Every common multiple of "3" and "5" is printed as "FizzBuzz".
// for loops evaluate whether a quotient is an integer.
int main()
{
    float counterMain = 1;
    // factorMain will be set to the multiples for Fizz or "Buzz.
    float factorMain = 0;
    // factorTest "tests" factorMain to see if it is an integer factor of counterMain.
    float factorTest = 1;
    // factorLimit sets the divisor (factor) upper limit (only one other factor in this case).
    // factorLimit is a quotient of factorMain and factorTest.
    float factorLimit = 0;
    int passFizz = 0;
    int passBuzz = 0;

    // counterMain gets printed at the end if nothing is passed to checkFizzBuzz.
    for(counterMain = 1; counterMain <= 100; ++counterMain)
    {
        // Resets pass check.
        passFizz = 0;
        passBuzz = 0;
        // "Fizz" value set here.
        factorMain = 3;
        // Sets the divisor upper limit as a quotient of counterMain.
        factorLimit = counterMain / factorMain;

        //factorTest counts to the divisor upper limit.
        for(factorTest = 1; factorTest <= factorLimit; ++factorTest)
        {
            // Tests if factorTest is an integer quotient of counterMain divided by factorMain.
            if(factorTest == factorLimit)
            {
                // Passes 1 to passFizz if factorMain is an integer.
                passFizz = 1;
                // Sets factorTest to for loop exit condition.
                factorTest = factorLimit;
            }
        }

        // "Buzz" value is set here.
        factorMain = 5;
        factorLimit = counterMain / factorMain;
        // Copy of the above for loop but passes 2 to passBuzz instead.
        for(factorTest = 1; factorTest <= factorLimit; ++factorTest)
        {
            if(factorTest == factorLimit)
            {
                passBuzz = 2;
                factorTest = factorLimit;
            }
        }

        // Fizz-Buzz pass check, then prints out.
        int checkFizzBuzz = (passFizz + passBuzz);
        // Value of 0 means no factors were found, prints counterMain value instead.
        if(checkFizzBuzz == 0)
        {
            std::cout << counterMain;
        }
        if(checkFizzBuzz == 1)
        {
            std::cout << "Fizz";
        }
        if(checkFizzBuzz == 2)
        {
            std::cout << "Buzz";
        }
        if(checkFizzBuzz == 3)
        {
            std::cout << "FizzBuzz";
        }
        std::cout << "\n";
    // Return to counterMain for loop.
    }

    return 0;
}
view (82 lines, 100 lines of output)
pasted on Sep 28:
#include <iostream>

// Prints numbers "1" through "100" as integers.
// Every multiple of "3" is printed as "Fizz".
// Every multiple of "5" is printed as "Buzz".
// Every common multiple of "3" and "5" is printed as "FizzBuzz".
// for loops evaluate whether a quotient is an integer.
int main()
{
    float counterMain = 1;
    // factorMain will be set to the multiples for Fizz or "Buzz.
    float factorMain = 0;
    // factorTest "tests" factorMain to see if it is an integer factor of counterMain.
    float factorTest = 1;
    // factorLimit sets the divisor (factor) upper limit (only one other factor in this case).
    // factorLimit is a quotient of factorMain and factorTest.
    float factorLimit = 0;
    int passFizz = 0;
    int passBuzz = 0;

    // counterMain gets printed at the end if nothing is passed to checkFizzBuzz.
    for(counterMain = 1; counterMain <= 100; ++counterMain)
    {
        // Resets pass check.
        passFizz = 0;
        passBuzz = 0;
        // "Fizz" value set here.
        factorMain = 3;
        // Sets the divisor upper limit as a quotient of counterMain.
        factorLimit = counterMain / factorMain;

        //factorTest counts to the divisor upper limit.
        for(factorTest = 1; factorTest <= factorLimit; ++factorTest)
        {
            // Tests if factorTest is an integer quotient of counterMain divided by factorMain.
            if(factorTest == factorLimit)
            {
                // Passes 1 to passFizz if factorMain is an integer.
                passFizz = 1;
                // Sets factorTest to for loop exit condition.
                factorTest = factorLimit;
            }
        }

        // "Buzz" value is set here.
        factorMain = 5;
        factorLimit = counterMain / factorMain;
        // Copy of the above for loop but passes 2 to passBuzz instead.
        for(factorTest = 1; factorTest <= factorLimit; ++factorTest)
        {
            if(factorTest == factorLimit)
            {
                passBuzz = 2;
                factorTest = factorLimit;
            }
        }

        // Fizz-Buzz pass check, then prints out.
        int checkFizzBuzz = (passFizz + passBuzz);
        // Value of 0 means no factors were found, prints counterMain value instead.
        if(checkFizzBuzz == 0)
        {
            std::cout << counterMain;
        }
        if(checkFizzBuzz == 1)
        {
            std::cout << "Fizz";
        }
        if(checkFizzBuzz == 2)
        {
            std::cout << "Buzz";
        }
        if(checkFizzBuzz == 3)
        {
            std::cout << "FizzBuzz";
        }
        std::cout << "\n";
    // Return to counterMain for loop.
    }

    return 0;
}
view (82 lines, 100 lines of output)


PHP:
pasted on Apr 22:
1
2
3
4
5
6
7
8
<?php

for($i=1;$i<=100;$i++){
if ($i%3==0) echo "Fizz";
if ($i%5==0) echo "Buzz";
if ($i%3 >0 && $i%5 > 0) echo $i;
echo "\n";
}
view (8 lines, 100 lines of output)


Perl:
pasted on Aug 13:
1
2
3
4
5
6
(print ((plorg($_).plib($_)) or ($_))) and print "\n" for (1..100);



sub plorg{$_%3? '':'Fizz'}
sub plib{$_%5? '':'Buzz'}
view (6 lines, 100 lines of output)
pasted on Aug 2:
1
2
3
4
5
6
@FB1 = (1..100);
@FB2 = map{!($_%3 or $_%5)?'FizzBuzz': $_}@FB1;
@FB3 = map{(/\d/ and !($_%3))?'Fizz':$_}@FB2;
@FB4 = map{(/\d/ and !($_%5))?'Buzz':$_}@FB3;
@FB5 = map{$_."\n"}@FB4;
print @FB5;
view (6 lines, 100 lines of output)


Python:
pasted on Aug 22:
1
2
for i in range(1,101) :
    print("Fizz"*(i%3<1)+(i%5<1)*"Buzz"or i)
view (2 lines, 100 lines of output)
pasted on Aug 1:
1
2
3
4
5
6
7
8
9
for i in range(1, 101):
    if (i % 15 == 0):
        print "FizzBuzz"
    elif (i%3 == 0):
        print "Fizz"
    elif (i%5 == 0):
        print "Buzz"
    else:
        print i
view (9 lines, 100 lines of output)


Ruby:
pasted on Jul 12:
1
2
3
4
5
6
7
8
9
10
11
12
def fizz_buzz(numbers)
    
    numbers.each do |number|
       response = ""
       response += "Fizz" if number % 3 == 0
       response += "Buzz" if number % 5 == 0
       puts response.empty? ? number : response
    end
end

numbers = (1..100).to_a
fizz_buzz(numbers)
view (12 lines, 100 lines of output)