[ create a new paste ] login | about

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

C:
fargus3000 - pasted on Jul 26:
#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 Mar 15:
#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)


C++:
pasted on Feb 9:
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)


Perl:
pasted on Mar 22:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(1..100){
    print "Fizz" if it_is_divisible_by_3 ($_);
    print "Buzz" if it_is_divisible_by_5 ($_);
    print "$_" if it_is_not_divisible_by_3_or_5 ($_);
    print "\n" # new line!
}

sub it_is_divisible_by_3{
    !($_[0]%3)
}
sub it_is_divisible_by_5{
    !($_[0]%5)
}
sub it_is_not_divisible_by_3_or_5 {
    $_[0]%3 and $_[0]%5
}
view (16 lines, 100 lines of output)
pasted on Mar 16:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# @FB1 is an array of numbers that goes from 1 to 100
    @FB1 = (1..100);

# @FB2 is that first array 
# with all the values divisible by 5 and 3
# transformed into "FizzBuzz"
    @FB2 = map{!($_%3 or $_%5)?'FizzBuzz': $_}@FB1;

# @FB3 is all the remaining numbers divisible by 3
# transformed into Fizz
    @FB3 = map{(/\d/ and !($_%3))?'Fizz':$_}@FB2;

# @FB4 is all the remaining numbers divisible by 5
# transformed into "Buzz"
    @FB4 = map{(/\d/ and !($_%5))?'Buzz':$_}@FB3;

# @FB5 is all values with a carriage return added to the end
    @FB5 = map{$_."\n"}@FB4;

print @FB5; #kinda' obvious
view (20 lines, 100 lines of output)


Python:
pasted on Apr 1:
1
2
3
4
5
6
7
8
9
10
for i in range(1,101):
    txt = ''
    if i%3==0:
        txt += 'Fizz'
    if i%5==0:
        txt += 'Buzz'
    if txt == '':
        print(i)
    else:
        print(txt)
view (11 lines, 100 lines of output)
pasted on Apr 1:
1
2
3
4
5
6
7
8
9
for i in range(1,101):
    if i%15==0:
        print('FizzBuzz')
    elif i%5==0:
        print("Buzz")
    elif i%3==0:
        print('Fizz')
    else:
        print(i)
view (9 lines, 100 lines of output)