[ create a new paste ] login | about

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

C:
pasted on Nov 26:
#include <stdio.h>

#define FIZZ "Fizz"
#define BUZZ "Buzz"
int main( int argc, char **argv )
{
	int idx;
        char *out;

	for ( idx = 1; idx <= 100; idx++ )
	{
		if ( idx % 3 == 0 && idx % 5 != 0 )
		{
			out = FIZZ;
		}
		else if ( idx % 3 != 0 && idx % 5 == 0 )
		{
			out = BUZZ;
		}
		else if ( idx % 3 == 0 && idx % 5 == 0 )
		{
			out = FIZZ BUZZ;
		}
		else
		{
			out = NULL;
		}

		if ( out == NULL )
		{
			printf( "%d\n", idx );
		}
		else
		{
			printf( "%s\n", out );
		}
	}
	return 0;
}
view (39 lines, 100 lines of output)
pasted on Nov 19:
#include <stdio.h>
int main()
{
    int i; for (i = 1; i <= 100; i++)
    {
        if (((i % 5) == 0) && ((i % 3) == 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 (30 lines, 100 lines of output)


C++:
pasted 10 hours ago:
#include <iostream>
class fizzbuzz
{
public:
    static void go()
    {
        for (int i = 1; i < 101; i++)
        {
            if (i%15 == 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;
        }
    }
};
int main()
{
    fizzbuzz::go();
}
view (23 lines, 100 lines of output)
pasted 10 hours ago:
#include <iostream>
#include <string>
#include <algorithm>
#define FIZZBUZZ_FACTOR 15
class fizzbuzz {
public:
    static void go() {
        int cur = 0;
        for (int i = 1; i < 101; i++) {
            if (cur < FIZZBUZZ_FACTOR) {
                if (answers[cur].empty()) {
                    std::cout<<i<<std::endl;
                    cur++;
                }
                else {
                    std::cout<<answers[cur++]<<std::endl;
                }
            }
            if (cur == FIZZBUZZ_FACTOR) {
                cur = 0;
            }
        }
    }
private:
    static std::string answers[FIZZBUZZ_FACTOR];
};
std::string fizzbuzz::answers[FIZZBUZZ_FACTOR] = 
{
    "", "", "Fizz", "", "Buzz",
    "Fizz", "", "", "Fizz", "Buzz",
    "", "Fizz", "", "", "FizzBuzz"
};
int main() {
    fizzbuzz::go();
}
view (35 lines, 100 lines of output)


Haskell:
pasted on Nov 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)
pasted on Nov 7:
1
2
3
import Control.Monad
import System.Random
main=forM_ [1..100](\n->(when(rem n 15==1)(setStdGen(mkStdGen 219279162)))>>(getStdRandom(randomR(0,3))>>=f n))where f i n=putStrLn([show i,"Fizz","Buzz","FizzBuzz"]!!n)
view (3 lines, 100 lines of output)


Lua:
pasted on Nov 8:
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 Aug 19:
print([[
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
]])
view (102 lines, 101 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:
osaze - pasted on Nov 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?
for ($i=1; $i<=100; $i++) {
    if ($i%3==0 && $i%5==0) {
        echo 'FizzBuzz';
    }else if($i%3==0){
        echo 'Fizz';
    }else if($i%5==0){
        echo 'Buzz';
    }else{
        echo $i;
    }
    echo "\n";
}
view (13 lines, 100 lines of output)
pasted on Nov 9:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?
for ($i=1; $i<=100; $i++) {
    if ($i%3==0 && $i%5==0) {
        echo 'FizzBuzz';
    }else if($i%3==0){
        echo 'Fizz';
    }else if($i%5==0){
        echo 'Buzz';
    }else{
        echo $i;
    }
    echo "\n";
}
view (13 lines, 100 lines of output)


Perl:
pasted on Oct 15:
1
2
3
4
5
6
7
8
9
10
11
12
13
my $x = false, $y = false; 

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


Python:
pasted 11 hours ago:
1
2
3
4
5
for y in range(1,101):    
  if(y % 3 == 0 and y % 5 == 0): print "fizzbuzz"
  elif(y % 5 == 0): print "buzz"
  elif(y % 3 == 0): print "fizz"
  else: print y
view (5 lines, 100 lines of output)
pasted 11 hours ago:
1
2
3
4
5
for y in range(1,101):    
  if(y % 3 == 0 and y % 5 == 0): print "fizzbuzz"
  elif(y % 5 == 0): print "buzz"
  elif(y % 3 == 0): print "fizz"
  else: print y
view (5 lines, 100 lines of output)


Ruby:
pasted on Nov 18:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for n in 1..100 do
    by_three = n%3==0
    by_five = n%5==0
    case
        when by_three&&by_five
            puts 'fizzbuzz'
        when by_three
            puts 'fizz'
        when by_five
            puts 'buzz'
        else
            puts n
    end
end
view (14 lines, 100 lines of output)
pasted on Nov 18:
1
1.upto(?d){|e|puts ["%sBuzz"%x=[:Fizz][e%3]][e%5]||x||e}
view (1 line, 100 lines of output)