[ create a new paste ] login | about

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

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

void main(void)
{
    int i;
    for(i = 1; i <= 100; i ++) {
        if((i % 5 == 0) && (i % 3 == 0)) {
            printf("FizzBuzz\n");
        } else if (i % 5 == 0) {
            printf("Buzz\n"); 
        } else if (i % 3 == 0) {
            printf("Fizz\n"); 
        } else {
            printf("%d\n", i); 
        }
    }
    return;
}
view (18 lines, 100 lines of output)
pasted on Feb 15:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

void main(void)
{
    int i;
    for(i = 1; i <= 100; i ++) {
        if((i % 5 == 0) && (i % 3 == 0)) {
            printf("FizzBuzz\n");
        } else if (i % 5 == 0) {
            printf("Buzz\n"); 
        } else if (i % 3 == 0) {
            printf("Fizz\n"); 
        } else {
            printf("%d\n", i); 
        }
    }
    return;
}
view (18 lines, 100 lines of output)


C++:
pasted on Jan 11:
#include <iostream>

int main() {
for (int i = 1; i <= 100; i++) {
    bool fizz = (i % 3) == 0;
    bool buzz = (i % 5) == 0;

    if (fizz) {
        std::cout << "Fizz";
    }

    if (buzz) {
        std::cout << "Buzz";
    }

    if (!fizz && !buzz) {
        std::cout << i;
    }

    std::cout << '\n';
}
return 0;
}
view (23 lines, 100 lines of output)
pasted on Jan 10:
1
2
3
4
5
6
7
8
9
10
11
int main() {
	for (int i = 1; i <= 100; i++) {
		bool isFizz = (i % 3 == 0);
		bool isBuzz = (i % 5 == 0);
		if (isFizz || isBuzz) {
			printf("%s%s\n", (isFizz ? "fizz" : ""), (isBuzz ? "buzz" : ""));
		} else {
			printf("%d\n", i);
		}
	}
}
view (11 lines, 100 lines of output)


Haskell:
pasted on Dec 29:
1
2
3
4
5
6
7
  subst (i, a)
    | i `mod` 15 == 0 = putStrLn "FizzBuzz"
    | i `mod` 5 == 0 = putStrLn "Buzz"
    | i `mod` 3 == 0 = putStrLn "Fizz"
    | otherwise = a
  makePrint i = (i, putStrLn $ show i)
  main = sequence_ $ map (subst . makePrint) [1..100]
view (7 lines, 100 lines of output)
pasted on Dec 12:
1
2
3
4
5
6
7
8
fizz :: Int -> String
fizz n | n `mod` 15 == 0  = "FizzBuzz"
       | n `mod` 3  == 0  = "Fizz"
       | n `mod` 5  == 0  = "Buzz"
       | otherwise        = show n
 
main :: IO()
main = mapM_ putStrLn $ map fizz [1..100]
view (8 lines, 100 lines of output)


Lua:
pasted on Feb 14:
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)
pasted on Nov 4:
1
2
3
4
t = {[0] = "fizzbuzz", [6] = "fizz", [10] = "buzz"}  -- this is
for i = 1, 100 do      -- a simple example of a right-justified
    print(t[(i % 15)^4 % 15] or i)  -- comment after code which
end                  -- has a variety of different line lengths
view (4 lines, 100 lines of output)


PHP:
clambercy - pasted on Dec 17:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

for ($i = 1; $i <= 100; $i++) {
    $out = '';
    if ($i % 3 == 0) {
        $out = "Fizz";
    } 
    if ($i % 5 == 0) {
        $out .= "Buzz";
    }
    
    echo ($out ? $out : $i) . PHP_EOL;
}


?>
view (16 lines, 100 lines of output)
clambercy - pasted on Dec 17:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

for ($i = 1; $i <= 100; $i++) {
    $out = '';
    if ($i % 3 == 0) {
        $out = "Fizz";
    } 
    if ($i % 5 == 0) {
        $out .= "Buzz";
    }
    if ($out == '') {
        $out = $i;
    } 
    echo ($out ? $out : $i) . PHP_EOL;
}


?>
view (18 lines, 100 lines of output)


Perl:
pjvangemmeren - pasted on Oct 12:
1
 print+(Fizz)[$_%3].(Buzz)[$_%5]||$_,$/for 1..100
view (1 line, 100 lines of output)
pasted on Oct 7:
1
 print+(Fizz)[$_%3].(Buzz)[$_%5]||$_,$/for 1..100
view (1 line, 100 lines of output)


Python:
pasted on Feb 14:
1
2
3
4
5
for i in range(1,101):
    if i%15 == 0: print("fizzbuzz")
    elif i%3 == 0: print("fizz")
    elif i%5 == 0: print("buzz")
    else: print(i)
view (5 lines, 100 lines of output)
pasted on Jan 17:
1
2
3
4
5
6
7
8
9
for x in range(1,101):
    if x % 15 == 0:
        print "FizzBuzz" # Catch multiples of both first.
    elif x % 3 == 0:
        print "Fizz"
    elif x % 5 == 0:
        print "Buzz"
    else:
        print x
view (9 lines, 100 lines of output)


Ruby:
pasted 5 hours ago:
1
2
3
4
5
6
7
8
9
10
11
for i in 1..100
    if i % 15 == 0
        puts 'fizzbuzz'
    elsif i % 5 == 0
        puts 'buzz'
    elsif i % 3 == 0
        puts 'fizz'
    else
        puts i
    end
end 
view (11 lines, 100 lines of output)
pasted on Feb 4:
1
2
3
4
5
6
7
8
9
10
11
for i in 1..100
    if i % 15 == 0
        puts 'fizzbuzz'
    elsif i % 5 == 0
        puts 'buzz'
    elsif i % 3 == 0
        puts 'fizz'
    else
        puts i
    end
end 
view (11 lines, 100 lines of output)