[ create a new paste ] login | about

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

C:
pasted on Aug 28:
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <string.h>

main()
{
    int i;
    for( i = 1; i<=100; i++) ((i%3)&&(i%5))?printf("%i\n",i):printf("%s%s\n",i%3?"":"fizz",i%5?"":"buzz");
    return 0;
}
view (9 lines, 100 lines of output)
pasted on Aug 28:
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <string.h>

main()
{
    int i;
    for( i = 1; i<=100; i++) ((i%3)&&(i%5))?printf("%i\n",i):printf("%s%s\n",i%3?"":"fizz",i%5?"":"buzz");
    return 0;
}
view (9 lines, 100 lines of output)


C++:
pasted on Aug 28:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=1;i<=100;i++)
{
    if(i%3==0 && i%5==0)
        cout<<"FizzBuzz"<<endl;
    else if(i%3==0)
        cout<<"Fizz"<<endl;
    else if(i%5==0)
        cout<<"Buzz"<<endl;
    else
        cout<<i<<endl;
}
return 0;
}
 
view (20 lines, 100 lines of output)
pasted on Aug 22:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
int i;
for(i=1;i<=100;i++)
{
    if(i%3==0 && i%5==0)
        cout<<"FizzBuzz"<<endl;
    else if(i%3==0)
        cout<<"Fizz"<<endl;
    else if(i%5==0)
        cout<<"Buzz"<<endl;
    else
        cout<<i<<endl;
}
return 0;
}
view (16 lines, 100 lines of output)


Haskell:
pasted on Aug 27:
1
2
3
4
5
6
7
8
fizzbuzz :: Integer -> String
fizzbuzz x
  | x `mod` 5 == 0 && x `mod` 3 == 0 = "fizzbuzz"
  | x `mod` 5 == 0 = "buzz"
  | x `mod` 3 == 0 = "fizz"
  | otherwise = show x

main = sequence $ (putStrLn .) fizzbuzz `map` [1 .. 100]
view (8 lines, 100 lines of output)
pasted on Aug 22:
1
2
3
4
5
6
7
8
main = do
  putStr . unlines $ map fizzbuzz [1..100]

fizzbuzz n
  | mod n 15 == 0 = "Fizzbuzz"
  | mod n 3 == 0 = "Fizz"
  | mod n 5 == 0 = "Buzz"
  | otherwise = show n
view (8 lines, 100 lines of output)


Lua:
pasted on Aug 19:
print([[
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
]])
view (102 lines, 101 lines of output)
pasted on Aug 19:
1
for i=1,100 do print(i%15<1 and"FizzBuzz"or i%3<1 and"Fizz"or i%5<1 and"Buzz"or i)end
view (1 line, 100 lines of output)


PHP:
pasted yesterday:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

$i = 1;

while ($i <= 100) {
    if ($i % 3 == 0 && $i % 5 == 0) echo "FizzBuzz";
    elseif ($i % 5 == 0) echo "Buzz";
    elseif ($i % 3 == 0) echo "Fizz";
    else echo $i;

    echo "\n";
    $i++;
}

?>
view (15 lines, 100 lines of output)
pasted on Aug 30:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
  for ($x=1; $x<=100; $x++) {
     if ($x%15==0) {
       echo "FizzBuzz\n";
     }
     else if ($x%3==0) {
       echo "Fizz\n";
     }
     else if ($x%5==0) {
       echo "Buzz\n";
     }
     else {
       echo ($x."\n");
     }
  }
?>
view (16 lines, 100 lines of output)


Perl:
pasted on Aug 27:
1
2
3
4
5
6
7
8
9
10
11
12
print sub {
	my @out;
	for (1..100) {
		push @out, do {
			if ($_ % 3 == 0 and $_ % 5 == 0) { "fizzbuzz"
			} elsif ($_ % 3 == 0) { "fizz"
			} elsif ($_ % 5 == 0) {	"buzz"
			} else { $_ }
		};
	}
	return join "\n", @out;
}->();
view (12 lines, 100 lines of output)
pasted on Aug 27:
1
2
3
4
5
6
7
8
9
10
11
12
print sub {
	my @out;
	for (1..100) {
		push @out, do {
			if ($_ % 3 == 0 and $_ % 5 == 0) { "fizzbuzz"
			} elsif ($_ % 3 == 0) { "fizz"
			} elsif ($_ % 5 == 0) {	"buzz"
			} else { $_ }
		};
	}
	return join "\n", @out;
}->();
view (12 lines, 100 lines of output)


Python:
pasted on Aug 25:
1
2
for i in range(1, 101):
    print "FizzBuzz" if i % 3 is 0 and i % 5 is 0 else "Fizz" if i % 3 is 0 else "Buzz" if i % 5 is 0 else i
view (2 lines, 100 lines of output)
pasted on Aug 20:
1
2
3
4
5
6
7
8
9
10
11
12
def fizzbuzz(number):
        if number % 3 == 0 and number % 5 == 0:
                return 'FizzBuzz'
        elif number % 3 == 0:
                return 'Fizz'
        elif number % 5 == 0:
                return 'Buzz'
        else:
                return number

for number in range(1,101):
        print fizzbuzz(number)
view (12 lines, 100 lines of output)


Ruby:
pasted 7 hours ago:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
FIZZ = 3
BUZZ = 5
def FizzBuzz
  $number = 1
  while $number <= 100 do
    if $number % FIZZ == 0
      print "Fizz"
    end
    if $number % BUZZ == 0
      print "Buzz"
    end 
    if $number % FIZZ != 0 && $number % BUZZ != 0
      print $number
    end
    $number += 1
    puts ""
  end
end

FizzBuzz()
view (20 lines, 100 lines of output)
pasted on Aug 29:
1
2
3
4
5
6
7
8
9
10
11
for num in 1..100 do
  if num % 15 == 0
    puts "FizzBuzz"
  elsif num % 5 == 0
    puts "Buzz"
  elsif num % 3 == 0
    puts "Fizz"
  else
    puts num
  end
end
view (11 lines, 100 lines of output)


Scheme:
pasted on May 30:
1
2
3
4
5
6
7
8
(do ((i 1 (+ i 1)))
    ((> i 100))
    (display
      (cond ((= 0 (modulo i 15)) "FizzBuzz")
            ((= 0 (modulo i 3))  "Fizz")
            ((= 0 (modulo i 5))  "Buzz")
            (else                i)))
    (newline))
view (8 lines, 100 lines of output)