[ create a new paste ] login | about

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

C:
pasted on Jun 12:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main (void) {
    int i;

    for (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 (18 lines, 100 lines of output)
pasted on Jun 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>

main(){
    int i;
    int num = 100;

    for(i = 1; i <= num; i++){
        if(i%3 == 0){
            printf("fizz");
        }
        if(i%5 == 0){
            printf("buzz");
        }
        if(i%3 != 0 && i%5 != 0){
            printf("%d", i);
        }
        printf("\n");        
    }

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


C++:
pasted on Jun 9:
#include <iostream>

using namespace std;

void fizzbuzz1()
{
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		string output = "";
		output += ( ch % 3 == 0 ) ? "fizz":"";
		output += ( ch % 5 == 0 ) ? "buzz":"";
		
		if( output.size() == 0 )
		{
			cout << +ch;
		}
		else
		{
			cout << output;
		}
		cout <<  "\n";
	}
}

void fizzbuzz2()
{
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		bool flg = false;
		if( ch % 3 == 0 )
		{
			cout << "fizz";
			flg = true;
		}
		if( ch % 5 == 0 )
		{
			cout << "buzz";
			flg = true;
		}
		
		if( !flg )
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

void fizzbuzz3()
{
	const char* outputs[]  = { "", "fizz", "buzz", "fizzbuzz" };
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		unsigned char index = 0;
		if( ch % 3 == 0 )
		{
			index++;
		}
		if( ch % 5 == 0 )
		{
			index+=2;
		}
		
		if( index > 0 )
		{
			cout << outputs[index];
		}
		else
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

void fizzbuzz4()
{
	const char* outputs[]  = { "", "fizz", "buzz", "fizzbuzz" };
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		unsigned char index = 0;
		index += ( ch % 3 == 0 );
		index += ( ch % 5 == 0 )*2;

		if( index > 0 )
		{
			cout << outputs[index];
		}
		else
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

int main()
{
     fizzbuzz4();
    
    return 0;
}
view (102 lines, 100 lines of output)
pasted on Jun 9:
#include <iostream>

using namespace std;

void fizzbuzz1()
{
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		string output = "";
		output += ( ch % 3 == 0 ) ? "fizz":"";
		output += ( ch % 5 == 0 ) ? "buzz":"";
		
		if( output.size() == 0 )
		{
			cout << +ch;
		}
		else
		{
			cout << output;
		}
		cout <<  "\n";
	}
}

void fizzbuzz2()
{
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		bool flg = false;
		if( ch % 3 == 0 )
		{
			cout << "fizz";
			flg = true;
		}
		if( ch % 5 == 0 )
		{
			cout << "buzz";
			flg = true;
		}
		
		if( !flg )
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

void fizzbuzz3()
{
	const char* outputs[]  = { "", "fizz", "buzz", "fizzbuzz" };
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		unsigned char index = 0;
		if( ch % 3 == 0 )
		{
			index++;
		}
		if( ch % 5 == 0 )
		{
			index+=2;
		}
		
		if( index > 0 )
		{
			cout << outputs[index];
		}
		else
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

void fizzbuzz4()
{
	const char* outputs[]  = { "", "fizz", "buzz", "fizzbuzz" };
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		unsigned char index = 0;
		index += ( ch % 3 == 0 );
		index += ( ch % 5 == 0 );

		if( index > 0 )
		{
			cout << outputs[index];
		}
		else
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

int main()
{
     fizzbuzz3();
    
    return 0;
}
view (102 lines, 100 lines of output)


Haskell:
pasted on Jan 16:
1
2
3
4
5
6
7
8
9
import Data.List

main :: IO ()
main = mapM_ putStrLn $ take 100 fizzbuzz

fizzbuzz :: [String]
fizzbuzz = zipWith3 (\n fizz buzz ->
                     if null (fizz++buzz) then show (n::Int) else fizz++buzz)
             [1..] (cycle ["","","Fizz"]) (cycle ["","","","","Buzz"])
view (9 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 Jun 5:
1
2
3
4
5
6
for (1..100){
    my$text;
    $text .= 'Fizz' unless $_%3;
    $text .= 'Buzz' unless $_%5;
    $text? print "$text\n" : print "$_\n";
}
view (7 lines, 100 lines of output)
pasted on Apr 30:
1
2
3
4
5
6
7
8
9
@a = ('',"Fizz","Buzz","FizzBuzz");


for(1..100){
    $a[0]=$_; 
    print $a[!($_%3) + !($_%5)*2];
    print "\n";
} 
    
view (9 lines, 100 lines of output)


Python:
raj@cs.gsu.edu - pasted on Jun 4:
1
2
3
4
5
6
7
8
9
10
for n in xrange(1,101):

    if n % 5 ==0 and n % 3 == 0:
        print "Fizzbuzz"
    elif n % 3 == 0:
        print "Fizz"
    elif n % 5 == 0:
        print "Buzz"
    else:
        print n
view (10 lines, 100 lines of output)
pasted on May 20:
1
2
3
4
5
6
7
8
9
10
for n in xrange(1,101):

    if n % 5 ==0 and n % 3 == 0:
        print "Fizzbuzz"
    elif n % 3 == 0:
        print "Fizz"
    elif n % 5 == 0:
        print "Buzz"
    else:
        print n
view (10 lines, 100 lines of output)


Ruby:
pasted on Feb 7:
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)