[ create a new paste ] login | about

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

C:
pasted on Nov 2:
int main(void) {
    int i;
    int isFizz, isBuzz, isFizzBuzz;

    for(i = 1; i <= 100; i++) {

        /* The following code is NOT tested or recommended... */

        isFizz     = (i % 3 == 0      ) ? 1 : 0;
        isBuzz     = (i % 5 == 0      ) ? 1 : 0;
        isFizzBuzz = (isFizz && isBuzz) ? 1 : 0;
{
        printf(
            (isFizz || isBuzz) ? "%s\n" : "%d\n",
            (
                isFizzBuzz ? "fizzbuzz" :
                isFizz     ? "fizz"     :
                isBuzz     ? "buzz"     :
                i
            )
        );
}
    }

    return 0;
}
view (26 lines, 100 lines of output)
pasted on Oct 27:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main() {  
    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)


C++:
pasted on Nov 15:
#include <iostream>

using namespace std;

int main()

{

	//iterating from 1 to 100

	for (int i = 1; i <= 100; i++)

	{

		if (i % 3 == 0 && i % 5 == 0) //if divisible by both 3 and 5

			cout << "FizzBuzz\n";

		else if (i % 3 == 0) //if divisible by only 3

			cout << "Fizz\n";

		else if (i % 5 == 0) //if divisible by only 5

			cout << "Buzz\n";

		else //if divisible by neither 3 nor 5

			cout << i << "\n";

	}

}
view (33 lines, 100 lines of output)
pasted on Nov 15:
#include <iostream>

using namespace std;

int main()

{

	//iterating from 1 to 100

	for (int i = 1; i <= 100; i++)

	{

		if (i % 3 == 0 && i % 5 == 0) //if divisible by both 3 and 5

			cout << "FizzBuzz\n";

		else if (i % 3 == 0) //if divisible by only 3

			cout << "Fizz\n";

		else if (i % 5 == 0) //if divisible by only 5

			cout << "Buzz\n";

		else //if divisible by neither 3 nor 5

			cout << i << "\n";

	}

}
view (33 lines, 100 lines of output)


Lua:
pasted on Sep 1:
1
2
3
4
5
6
7
8
9
for i = 1, 100 do
    result = (i % 3 == 0 and "Fizz" or "")..(i % 5 == 0 and "Buzz" or "")

    if result == "" then
        result = i
    end

    print(result)
end
view (9 lines, 100 lines of output)
pasted on Sep 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for i = 1, 100 do
    result = ""
    if i % 3 == 0 then
        result = result.."Fizz"
    end

    if i % 5 == 0 then
        result = result.."Buzz"
    end

    if result == "" then
        result = i
    end

    print(result)
end
view (16 lines, 100 lines of output)


Perl:
WillWest - pasted on Oct 31:
1
2
3
4
5
6
7
8
9
10
@a=(
sub{print "FizzBuzz\n"},
sub{print "Buzz\n"},
sub{print "Fizz\n"},
sub {print "$_[0]\n"}
);



$a[   ($_%3<=>0) + (2*($_%5<=>0))   ]->($_) for (1..100)
view (10 lines, 100 lines of output)
skeletonkey - pasted on Oct 20:
1
2
3
4
5
6
for my $i ( 1 .. 100 ) {
    print $i % 3 ? '':'fizz';
    print $i % 5 ? '':'buzz';
    print $i % 3 && $i % 5 ? $i : '';
    print "\n";
}
view (6 lines, 100 lines of output)


Python:
pasted on Nov 16:
1
2
3
4
5
6
7
8
9
for i in range(1, 101):
    if i % 3 == 0 and i%5 == 0:
        print("fizzbuzz")
    elif i % 3 == 0:
        print("fizz")
    elif i % 5 == 0:
        print("buzz")
    else:
        print(i)
view (9 lines, 100 lines of output)
dennyrolling - pasted on Nov 6:
1
2
3
4
5
for i in range(1, 101):
  if i % 3 == 0 and i % 5 == 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)