[ create a new paste ] login | about

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

C:
pasted on Feb 7:
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 <= 100; 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)
pasted on Jan 26:
#include <stdio.h>

int main (void)
{
int i=0;

	for(i=1; i<=100; i++)
	{
		if(i%15 == 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 (28 lines, 100 lines of output)


C++:
pasted on Feb 20:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main()
{
  for(int i=1;i<101;i++)
  {
      if(i%3==0) cout<<"Fizz";
      if(i%5==0) cout<<"Buzz";
      if(i%3!=0 && i%5!=0) cout<<i;
      cout<<endl;
  }
}
view (12 lines, 100 lines of output)
pasted on Feb 18:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main (void)
{
int x=0;
for(x=1; x<=100; x++)
{
if(x%15 == 0)
printf("fizzbuzz\n");
else if ((x%3)==0)
printf("fizz\n");
else if ((x%5)==0)
printf("buzz\n");
else 
printf("%d\n", x);
}
return 0;
}
view (17 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)
pasted on Nov 27:
1
2
3
4
5
6
7
import Data.List
fizzBuzzList :: Int -> [String]
fizzBuzzList n = zipWith3(\i f b -> if f++b == "" then show i else f++b)
    [1..n] (cycle ["","","Fizz"]) (cycle [ "", "", "", "", "Buzz"])

main :: IO()
main = mapM_ putStrLn $ fizzBuzzList 100
view (7 lines, 100 lines of output)


PHP:
pasted on Jan 14:
<?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 Feb 12:
1
2
3
4
5
6
7
8
my $text;
for (1..100){
    $text .= 'Fizz' unless $_%3;
    $text .= 'Buzz' unless $_%5;
    $text? print $text : print $_;
    print "\n";
    $text = '';
}
view (8 lines, 100 lines of output)
pasted on Feb 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
my $a = 1;

while($a < 101 ) {
        
        if($a % 15 == 0) {
                print("fizzbuzz" . "\n" );
        } elsif($a % 3 == 0) {
                print("fizz"."\n");
        }elsif($a % 5 == 0){
                print("buzz". "\n" );
        }else{
                 print ($a . "\n" );
        }
        $a = $a +1;

}
view (16 lines, 100 lines of output)


Python:
pasted on Feb 14:
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 Feb 12:
1
2
3
4
5
6
7
8
9
for i in range(1,101):
    if i % 3 == 0 and i % 5 == 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 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)