[ create a new paste ] login | about

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

C:
pasted on Nov 2:
/*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 Oct 21:
/*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 yesterday:
int main()
{
    for (int i = 1; i <= 100; ++i)
    {
        if (i % 3 == 0 && i % 5 == 0)
        {
            printf("FizzBuzz\n");
        }
        else if (i % 3 == 0)
        {
            printf("Fizz\n");
        }
        else if (i % 5 == 0)
        {
            printf("Buzz\n");
        }
        else
        {
            printf("%d\n", i);
        }
    }

    return 0;
}
view (26 lines, 100 lines of output)
pasted yesterday:
int main()
{
    for (int i = 1; i <= 100; ++i)
    {
        if (i % 3 == 0 && i % 5 == 0)
        {
            printf("FizzBuzz\n");
        }
        else if (i % 3 == 0)
        {
            printf("Fizz\n");
        }
        else if (i % 5 == 0)
        {
            printf("Buzz\n");
        }
        else
        {
            printf("%d\n", i);
        }
    }

}
view (25 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 Oct 28:
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)
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)


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)