[ create a new paste ] login | about

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

C:
pasted on Oct 3:
#include <stdio.h>
#include <stdbool.h>

//void fizzbuzz—Ź() {
//    int x = 0;
//    for(x = 1; x <= 100; ++x){
//    printf("%d ", x);
//    if(x%3 == 0) printf("Fizz");
//    if(x%5 == 0) printf("Buzz");
//    printf("\n");
//} 
//}


void fizzbuzz() {
    int y = 0;
    for(y = 1; y < 101; ++y){
        if(y%15 == 0) printf("FizzBuzz\n");
        else if(y%3 == 0) printf("Fizz\n");
        else if(y%5 == 0) printf("Buzz\n");
        else printf("%d\n", y);
    }
}

int main() {

   fizzbuzz(); 
   return 0;
}
view (29 lines, 100 lines of output)
pasted on Oct 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main()
{
	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 (18 lines, 100 lines of output)


C++:
pasted on Oct 14:
1
2
3
4
#include <cstdio>
int main() {
    for (int i=1; i<=100; ++i) i%3&&i%5?printf("%d\n",i):printf("%s%s\n",i%3?"":"Fizz",i%5?"":"Buzz");
}
view (4 lines, 100 lines of output)
pasted on Aug 18:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main() {
    for(int x = 1; x <= 100; x++) {        
        if(x % 15 == 0) {
            std::cout << "fizzbuzz\n";  
        }
        else if(x % 3 == 0) {
            std::cout << "fizz\n";
        }
        else if(x % 5 == 0) {
            std::cout << "buzz\n";
        }
        else {
            std::cout << x << std::endl;
        }
    }
    return 0;
}
view (19 lines, 100 lines of output, 3 comments)


PHP:
pasted on Oct 5:
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++) {

    if ($i % 3 == 0 && $i %  5 == 0) {
        echo "FizzBuzz\n";
        continue;
    }

    if ($i % 3 == 0) {
        echo "Fizz\n";
    } else if ($i % 5 == 0) {
        echo "Buzz\n"; 
    } else {
        echo "$i\n";
    }
    

}
view (18 lines, 100 lines of output)


Perl:
pasted on Jul 21:
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) + (2*!!($_%5))   ]->($_) for (1..100)
view (10 lines, 100 lines of output, 373 comments)
pasted on Jul 21:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
push @a, action ($_) for (1..100);

print map{$_->()."\n"}@a;

sub action{
   my $v = pop;

   sub {
      my $Narf;
      $Narf .= 'Fizz' unless $v%3;
      $Narf .= 'Buzz' unless $v%5;

      $Narf?$Narf:$v;
   }
}
view (15 lines, 100 lines of output, 3 comments)


Python:
pasted on Oct 28:
1
2
3
4
5
6
7
8
if __name__ == "__main__":
    for i in range(1,101):
        s = ""
        if i%3 == 0:
            s +="Fizz"
        if i%5 == 0:
            s += "Buzz"
        print(s if s else i)
view (8 lines, 100 lines of output)
pasted on Oct 5:
1
2
3
4
5
6
7
8
if __name__ == "__main__":
    for i in range(1,101):
        s = ""
        if i%3 == 0:
            s +="Fizz"
        if i%5 == 0:
            s += "Buzz"
        print(s if s else i)
view (8 lines, 100 lines of output)


Ruby:
pasted on Sep 30:
1
2
3
4
5
6
7
8
9
10
11
12
1.upto(100) do |x|
	if x % 3 == 0
		print "Fizz"
	end
	if x % 5 == 0
		print "Buzz"
	end
	if x % 3 != 0 && x % 5 != 0
		print x
	end
	print "\n"
end
view (12 lines, 100 lines of output)
pasted on Jun 28:
1
2
3
4
5
6
7
8
9
10
11
12
1.upto(100) do |x|
	if x % 3 == 0
		print "Fizz"
	end
	if x % 5 == 0
		print "Buzz"
	end
	if x % 3 != 0 && x % 5 != 0
		print x
	end
	print "\n"
end
view (12 lines, 100 lines of output, 3 comments)