[ create a new paste ] login | about

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

C:
pasted on Jul 27:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int main(void)
{
 int i;
for(i=1; i<=100; i++)
 {
   if(i%15== 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 (20 lines, 100 lines of output)
pasted on Jul 27:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main(void)
{
 int i;
for(i=1; i<=100; i++)
 {
   if(i%15== 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 (18 lines, 100 lines of output)


C++:
pasted on Aug 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

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

int main()
{
  for(int i=1;i<=100;i++)
  {
    string output = "";
    if(i%3==0) {output+="Fizz";}
    if(i%5==0) {output+="Buzz";}
    if((i%3!=0)&&(i%5!=0)) {cout<<i<<endl;}
    else {cout<<output<<endl;}
  }
return 0;
}
view (15 lines, 100 lines of output)


Haskell:
pasted on Aug 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 Aug 2:
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 Mar 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function fizzbuzz(n)
    if n%15 == 0 then
        print("fizzbuzz")
    elseif n%5 == 0 then
        print("buzz")
    elseif n%3 == 0 then
        print("fizz")
    else
        print("" .. n)
    end
end

for i=1,100,1 do
    fizzbuzz(i)
end
view (15 lines, 100 lines of output, 99 comments)


PHP:
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)
pasted on Apr 17:
1
print$_%15?$_%5?$_%3?$_:'Fizz':'Buzz':'FizzBuzz',"\n"for 1..100;
view (1 line, 100 lines of output, 99 comments)


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)