[ create a new paste ] login | about

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

C:
pasted yesterday:
1
2
3
4
5
6
7
8
9
#include <stdio.h>
int main(int argc, char *argv[])
{
    int i;
    for (i = 1; i < 101; ++i) {
        printf(!(i % 15) ? "fizzbuzz\n" : !(i % 5) ? "buzz\n" : !(i % 3) ? "fizz\n" : "%d\n", i);
    }
    return 0;
}
view (9 lines, 100 lines of output)
pasted yesterday:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main(int argc, char **argv){
    int i;
    for (i = 1; i <= 100; i++){
        if (i % 3){
            if (i % 5)
                printf("%d\n", i);
            else
                printf("Buzz\n");
        }else{
            if (i % 5)
                printf("Fizz\n");
            else
                printf("Fizzbuzz\n");
        }
    }
    return 0;
}
view (19 lines, 100 lines of output)


C++:
pasted on Feb 8:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main(void)
{
    for(uint i = 1; i <= 100; i++)
    {
        if(i%3 == 0 && i%5 ==0) {std:cout << "FizzBuzz" << std::endl;}
        else if(i%3 == 0) {std::cout << "Fizz" << std::endl;}
        else if(i%5 == 0) {std::cout << "Buzz" << std::endl;}
        else {std::cout << i << std::endl;}
    }

    return 0;
}
view (14 lines, 100 lines of output)
pasted on Feb 6:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
 
int main()
{
    int i, f = 2, b = 4; 
 
    for ( i = 1 ; i <= 100 ; ++i, --f, --b )
    {
        if ( f && b ) { std::cout << i;             }
        if ( !f )     { std::cout << "Fizz"; f = 3; }
        if ( !b )     { std::cout << "Buzz"; b = 5; }
        std::cout << std::endl;
    }
 
    return 0;
}
view (16 lines, 100 lines of output)


Haskell:
pasted on Dec 21:
1
main = mapM_ putStrLn $ map (\x -> if mod x 15 == 0 then "FizzBuzz" else if mod x 3 == 0 then "Fizz" else if mod x 5 == 0 then "Buzz" else show x) [1..100]
view (1 line, 100 lines of output)
pasted on Dec 16:
1
main = putStr . unlines $ map (\x -> if mod x 15 == 0 then "FizzBuzz" else if mod x 5 == 0 then "Buzz" else if mod x 3 == 0 then "Fizz" else show x) [1..100]
view (1 line, 100 lines of output)


Lua:
pasted 14 hours ago:
1
2
3
4
5
6
7
8
9
10
11
for i=1, 100 do
	if i % 3 == 0 and i % 5 == 0 then
		print("Fizzbuzz")
	elseif i % 3 == 0 then
		print("Fizz")
	elseif i % 5 == 0 then
		print("Buzz")
	else
		print(i)
	end
end
view (11 lines, 100 lines of output)
pasted 14 hours ago:
1
2
3
4
5
6
7
8
9
10
11
for i=1, 100 do
	if i % 3 == 0 and i % 5 == 0 then
		print("Fizzbuzz")
	elseif i % 3 == 0 then
		print("Fizz")
	elseif i % 5 == 0 then
		print("Buzz")
	else
		print(i)
	end
end
view (11 lines, 100 lines of output)


OCaml:
pasted on Oct 24:
1
2
3
4
5
6
7
8
9
10
11
let fizzbuzz index =
  match index mod 3, index mod 5 with
    0, 0 -> "FizzBuzz"
  | 0, _ -> "Fizz"
  | _, 0 -> "Buzz"
  | _    -> string_of_int index
 
let _ =
  for index = 1 to 100 do 
    print_endline (fizzbuzz index) 
  done
view (11 lines, 100 lines of output)
pasted on Oct 24:
1
2
3
4
5
6
7
8
9
let fizzbuzz index =
  match index mod 3, index mod 5 with
    0, 0 -> "FizzBuzz"
  | 0, _ -> "Fizz"
  | _, 0 -> "Buzz"
  | _    -> string_of_int index
 
let _ =
  for index = 1 to 100 do print_endline (fizzbuzz index) done
view (9 lines, 100 lines of output)


PHP:
pasted on Feb 24:
1
2
3
4
<?php
for ($i=1;$i<=100;$i++)
    echo (($x=($i%3?'':'Fizz').($i%5?'':'Buzz'))?$x:$i)."\n";
?>
view (4 lines, 100 lines of output)
pasted on Feb 19:
1
2
3
4
<?php
for ($i=1;$i<=100;$i++)
    echo (($x=($i%3?'':'Fizz').($i%5?'':'Buzz'))?$x:$i)."\n";
?>
view (4 lines, 100 lines of output)


Perl:
pasted on Feb 2:
1
2
3
4
5
6
7
8
9
10
11
for ($i = 1; $i < 101; $i++) {
    if ($i % 15 < 1) {
        print "FizzBuzz\n";
    } elsif ($i % 3 < 1) {
        print "Fizz\n";
    } elsif ($i % 5 < 1) {
        print "Buzz\n";
    } else {
        print "$i\n";
    }
}
view (11 lines, 100 lines of output)
pasted on Dec 24:
1
2
3
4
5
6
7
8
9
10
11
for ($i = 1; $i < 101; $i++) {
    if ($i % 15 < 1) {
        print "FizzBuzz\n";
    } elsif ($i % 3 < 1) {
        print "Fizz\n";
    } elsif ($i % 5 < 1) {
        print "Buzz\n";
    } else {
        print "$i\n";
    }
}
view (11 lines, 100 lines of output)


Python:
pasted 12 hours ago:
1
2
3
4
5
6
import random

for i in range(0, 100):
    if not i % 15:
        random.seed(1178741599)
    print [i+1, "Fizz", "Buzz", "FizzBuzz"][random.randint(0,3)]
view (6 lines, 100 lines of output)
pasted 12 hours ago:
1
2
3
4
5
6
7
import random

for i in range(0, 100):
    if not i % 15:
        random.seed(1178741599)
    print [i+1, "Fizz", "Buzz", "FizzBuzz"][random.randint(0,3)]
    
view (7 lines, 100 lines of output)


Ruby:
pasted on Feb 20:
1
puts((1..100).map{|m|a=m%3==0;p=m%5==0;(a||p)?((a)?%?Fizz?:%::)+((p)?%?Buzz?:%::):m})
view (1 line, 100 lines of output)
pasted on Feb 20:
1
(1..100).map{|m|a=m%3==0;p=m%5==0;print((a||p)?((a)?"Fizz":%{})+((p)?"Buzz":%{}):m);puts}
view (1 line, 100 lines of output)


Scheme:
pasted on Jan 19:
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)
pasted on Jan 2:
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)