[ create a new paste ] login | about

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

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

void main(){
    int i;
    for(i=1;i<101;i++){
        if(i % 3 == 0 && i % 5 != 0)
            printf("Fizz\n");
        else if(i % 3 != 0 && i % 5 == 0)
            printf("Buzz\n");
        else if(i %3 == 0 &&i % 5 == 0)
            printf("FizzBuzz\n",i);
        else
            printf("%d\n",i);
    }
}
view (15 lines, 100 lines of output)
pasted on Nov 17:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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 (19 lines, 100 lines of output)


C++:
pasted on Nov 14:
#include <iostream>
using namespace std;

int main(void){
    //カウント用の変数
    int i;

    for ( i = 1; i <= 100; i++){
        //3で割り切れるとき"Fizz"を出力
        if ( i % 3 == 0){
            cout << "Fizz";
        }
        //5で割り切れるとき"Buzz"を出力
        if ( i % 5 == 0){
            cout << "Buzz";
        }
        //3でも5でも割り切れないとき"数値"を出力
        if ( i % 3 && i % 5){
            cout << i;
        }
        //改行処理
        cout << endl;
    }
}
view (24 lines, 100 lines of output)
pasted on Nov 14:
#include <iostream>
using namespace std;

int main(void){
    //カウント用の変数
    int i;

    for ( i = 1; i <= 100; i++){
        //3で割り切れるとき"Fizz"を出力
        if ( i % 3 == 0){
            cout << "Fizz";
        }
        //5で割り切れるとき"Buzz"を出力
        if ( i % 5 == 0){
            cout << "Buzz";
        }
        //3でも5でも割り切れないとき"数値"を出力
        if ( i % 3 && i % 5){
            cout << i;
        }
        //改行処理
        cout << endl;
    }
}
view (24 lines, 100 lines of output)


Haskell:
pasted on Nov 5:
1
2
3
4
5
6
7
8
9
10
newtype FizzBuzz = MakeFizzBuzz Int

instance Show FizzBuzz where
  show (MakeFizzBuzz x)
    | (x `mod` 3 == 0) && (x `mod` 5 == 0)  =  "fizzbuzz"
    | (x `mod` 3 == 0)                      =  "fizz"
    | (x `mod` 5 == 0)                      =  "buzz"
    | otherwise                             =  show x

main = mapM_ print [MakeFizzBuzz x | x <- [1..100]]
view (10 lines, 100 lines of output)
pasted on Sep 4:
1
2
3
4
5
6
7
8
9
10
newtype FizzBuzz = MakeFizzBuzz Int

instance Show FizzBuzz where
  show (MakeFizzBuzz x)
    | (x `mod` 3 == 0) && (x `mod` 5 == 0)  =  "fizzbuzz"
    | (x `mod` 3 == 0)                      =  "fizz"
    | (x `mod` 5 == 0)                      =  "buzz"
    | otherwise                             =  show x

main = mapM_ print [MakeFizzBuzz x | x <- [1..100]]
view (10 lines, 100 lines of output)


Lua:
pasted on Oct 18:
1
2
3
4
5
6
7
8
for i=1, 100 do
    fizz = (i % 3 == 0)
    buzz = (i % 5 == 0)
    if fizz and buzz then print("FIZZBUZZ")
    elseif fizz then print("FIZZ")
    elseif buzz then print("BUZZ")
    else print(i) end
end
view (8 lines, 100 lines of output)
pasted on Oct 18:
1
2
3
4
5
6
7
8
for i=1, 100 do
    fizz = (i % 3 == 0)
    buzz = (i % 5 == 0)
    if (fizz) and (buzz) then print("FIZZBUZZ")
    elseif fizz then print("FIZZ")
    elseif buzz then print("BUZZ")
    else print(i) end
end
view (8 lines, 100 lines of output)


PHP:
pasted on Oct 14:
1
2
3
4
5
6
7
8
9
<?php
for($i=1;$i<=100;$i++){
$str = '';
if($i%3==0) { $str .= 'Fizz'; }
if($i%5==0) { $str .= 'Buzz'; }
if($str=='') { echo $i; } else { echo $str; }
echo "\n";
}
?>
view (9 lines, 100 lines of output)
pasted on Aug 1:
1
2
3
4
5
6
7
8
9
<?php
for($i=1;$i<=100;$i++){
$str = '';
if($i%3==0) { $str .= 'Fizz'; }
if($i%5==0) { $str .= 'Buzz'; }
if($str=='') { echo $i; } else { echo $str; }
echo "\n";
}
?>
view (9 lines, 100 lines of output)


Perl:
pasted on Aug 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (1..100){
    my $f = $_ % 3;
    my $b = $_ % 5;
    if( !$f ){
        print "fizz";
    }
    if( !$b ){
        print "buzz"
    }
    if( $f && $b ){
        print $_;
    }
    print "\n";
}
view (14 lines, 100 lines of output)


Python:
pasted on Nov 1:
1
2
3
4
5
6
7
8
9
for i in range(1, 101):
  if i % 15 == 0:
    print "FizzBuzz"
  elif i % 5 == 0:
    print "Buzz"
  elif i % 3 == 0:
    print "Fizz"
  else:
    print i
view (9 lines, 100 lines of output)
pasted on Nov 1:
1
2
3
4
5
6
def fizzbuzz(n):
    if n % 3 == 0 and n % 5 == 0: return 'FizzBuzz'
    elif n % 3 == 0: return 'Fizz'
    elif n % 5 == 0: return 'Buzz'
    else: return str(n)
print "\n".join(fizzbuzz(n) for n in xrange(1, 101))
view (6 lines, 100 lines of output)


Ruby:
pasted on Aug 15:
1
2
3
4
5
6
1.upto(100) do |i|
  out = nil
  out = out.to_s + 'Fizz' if i % 3 == 0
  out = out.to_s + 'Buzz' if i % 5 == 0
  puts out || i
end
view (6 lines, 100 lines of output)
pasted on Aug 15:
1
2
3
4
5
6
1.upto(100) do |i|
  out = nil
  out = out.to_s + 'Fizz' if i % 3 == 0
  out = out.to_s + 'Buzz' if i % 5 == 0
  puts out || i
end
view (6 lines, 100 lines of output)


Scheme:
pasted on Jun 26:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(define (fizz-buzzify index)
  (letrec ((fizz-val (lambda (i s) ;Hey, I found a use for letrec.
                       (if (> i 0)
                           (fizz-val (- i 1)
                                     (string-append
                                      (cond
                                        ((= (modulo i 15) 0) "FizzBuzz")
                                        ((= (modulo i 5) 0) "Buzz")
                                        ((= (modulo i 3) 0) "Fizz")
                                        (else (number->string i))) "\n" s))
                           s))))
    (fizz-val index "")))

(display (fizz-buzzify 100))
view (14 lines, 100 lines of output)