[ create a new paste ] login | about

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

C:
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)
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”.
*/

int main(int argc, char* argv[])
{
    int i;
    for (i = 1; i <= 100; i++)
    {
        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;
        }       
    }

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


C++:
pasted on Sep 9:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    for (int i = 1; i <= 100; ++i)
    {
        if (i%15 == 0)
            std::cout << "FizzBuzz\n";
        else if (i%3 == 0)
            std::cout << "Fizz\n";
        else if (i%5 == 0)
            std::cout << "Buzz\n";
        else
            std::cout << i << "\n"; 
    }
    return 0;
}
view (15 lines, 100 lines of output)
pasted on Sep 9:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    for (int i = 1; i <= 100; ++i)
    {
        if (i%15 == 0)
            std::cout << "FizzBuzz\n";
        else if (i%3 == 0)
            std::cout << "Fizz\n";
        else if (i%5 == 0)
            std::cout << "Buzz\n";
        else
            std::cout << i << "\n"; 
    }
}
view (14 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)
pasted on Mar 24:
<?php
for ($i = 1; $i <= 100; $i++)
{
	$output = '';

	if ($i % 3 == 0)
	{
		$output .= 'Fizz';
	}

	if ($i % 5 == 0)
	{
		$output .= 'Buzz';
	}

	if (!$output)
	{
		$output = $i;
	}

	echo $output . "\n";
}
?>
view (23 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)