[ create a new paste ] login | about

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

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

int main(int argc, char* argv[])
{
    int i;
    for (i = 1; i <= 100; i++)
    {
        if (i%3 == 0) printf("Fizz");
        if (i%5 == 0) printf("Buzz");
        if (i%3 && i%5) printf("%i", i);
        printf("\n");
    }
    return 0;
}
view (14 lines, 100 lines of output)
pasted on Mar 29:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

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


C++:
pasted on Mar 15:
#include <iostream>


using namespace std;




void fizz (int x) {


    


    if (x % 3 == 0 && (x % 5 == 0))


        cout << "FizzBuzz" << endl;


    


    else if (x % 3 == 0)


           cout << "Fizz" << endl;


           


           else if (x % 5 == 0) 


                   cout << "Buzz" << endl;


                   


                   else


                      cout << x << endl;


}


int main(){


    




    


    for (int i = 1; i <= 100; i++) {


        


        fizz(i);


        


           }


    


    


    return 0;


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


Haskell:
pasted yesterday:
1
2
3
4
5
6
7
8
9
10
11
fizzbuzz sx =
	[
		if x `mod` 15 == 0 then "FizzBuzz"
		else
			if x `mod` 3 == 0 then "Fizz"
			else
				if x `mod` 5 == 0 then "Buzz"
				else show x
		| x <- sx
	]
main = do sequence_ (map (putStr . (++ "\n")) (fizzbuzz [1 .. 100]))
view (11 lines, 100 lines of output)
pasted on Mar 23:
1
2
3
4
5
6
7
8
9
10
11
fizzbuzz sx =
	[
		if x `mod` 15 == 0 then "FizzBuzz"
		else
			if x `mod` 3 == 0 then "Fizz"
			else
				if x `mod` 5 == 0 then "Buzz"
				else show x
		| x <- sx
	]
main = do sequence_ (map (putStr . (++ "\n")) (fizzbuzz [1 .. 100]))
view (11 lines, 100 lines of output)


Lua:
pasted on Mar 19:
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 on Mar 2:
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 Mar 27:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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 (19 lines, 100 lines of output)
pasted on Mar 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?

for($i=1;$i<=100;$i++){
  if($i%15==0){
    print "FizzBuzz\n";
  }else if($i%3==0){
    print "Fizz\n";
  }else if($i%5==0){
    print "Buzz\n";
  }else{
    print $i."\n";
  }
}
view (13 lines, 100 lines of output)


Perl:
pasted on Mar 28:
1
2
3
4
5
6
7
for $i (1..100) {
$s = "";
if (($i % 3) == 0) {$s=$s."Fizz";}
if (($i % 5) == 0) {$s=$s."Buzz";}
if (length($s) == 0) {$s=$s."$i";}
print "$s\n";
}
view (7 lines, 100 lines of output)
pasted on Mar 16:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$n = 1;
while ($n <= 100) {
    $word = "";
    if ($n % 3 == 0) {
        $word .= "Fizz";
    }
    if ($n % 5 == 0) {
        $word .= "Buzz";
    }
    if (length $word == 0) {
        print "$n";
    } else {
        print $word;
    }
    print "\n";
    $n++;
}
view (17 lines, 100 lines of output)


Python:
pasted on Mar 19:
def fizzbuzz(n):
  n = int(n)
  if (n%15) == 0: return "FizzBuzz"
  elif (n%5) == 0: return "Buzz"
  elif (n%3) == 0: return "Fizz"
  else: return n

def fizzbuzz_decorator(func):
  def wrapper(*args, **kwargs):
    func(fizzbuzz(*args))
  return wrapper

def fizzbuzz_decorator2(func):
  def wrapper(*args, **kwargs):
    for n in range(1,args[0]):
      func(n)
  return wrapper

@fizzbuzz_decorator2
@fizzbuzz_decorator
def f(n):
  print n

f(101)
view (25 lines, 100 lines of output)
pasted on Mar 19:
def fizzbuzz(n):
  n = int(n)
  if (n%15) == 0: return "FizzBuzz"
  elif (n%5) == 0: return "Buzz"
  elif (n%3) == 0: return "Fizz"
  else: return n

class FizzBuzzIterator:
  def __init__(self,start, stop, step=1):
    self.count = start
    self.stop = stop
    self.step = step

  def __iter__(self):
    return self

  def next(self):
    if (self.step > 0 and self.count >= self.stop) or (self.step < 0 and self.count <= self.stop):
      raise StopIteration
    else:
      result = fizzbuzz(self.count)
      self.count += self.step
      return result

fbiter1 =  FizzBuzzIterator(1,101)
for fzbz in fbiter1: print fzbz
view (27 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)