[ create a new paste ] login | about

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

C:
pasted on Sep 13:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main(void) {
    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("%d", i);

        printf("\n");
    }
    return 0;
}
view (17 lines, 100 lines of output)
pasted on Sep 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main(void) {
    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("%d", i);

        printf("\n");
    }
    return 0;
}
view (17 lines, 100 lines of output)


C++:
pasted on Sep 12:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main() {

std::string buffer;

for(int i=1; i<=100; ++i){
  ostringstream convert;
  convert << i;
  buffer = convert.str();
  if(i%3==0) buffer = "Fizz";
  if(i%5==0) buffer = "Buzz";
  if(i%3==0 && i%5==0) buffer = "Fizzbuzz";
  std::cout << buffer << std::endl;
}

}
view (17 lines, 100 lines of output)
pasted on Sep 11:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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 (16 lines, 100 lines of output)


Haskell:
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)
pasted on Sep 1:
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 Sep 13:
1
2
3
4
5
6
7
8
9
10
11
12
13
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 (13 lines, 100 lines of output)
pasted on Sep 11:
1
2
3
4
5
6
7
8
9
10
11
12
13
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 (13 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 Aug 15:
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)
pasted on Aug 15:
1
2
3
4
5
6
7
8
9
10
11
12
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 (12 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)