[ create a new paste ] login | about

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

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

int main(int argc, char * argv[]) {
        int i, i_start = 1, i_end = 100;

        unsigned char flag = 0;
        for (i = i_start; i <= i_end; ++i) {
                flag = 0;
                if ((i % 3) == 0) {
                        flag += 1;
                }
                if ((i % 5) == 0) {
                        flag += 2;
                }
                if (flag) {
                        if (flag & 0x01) {
                                printf("Fizz");
                        }
                        if (flag & 0x02) {
                                printf("Buzz");
                        }
                }
                else {
                        printf("%d", i);
                }
                printf("\n");
        }
        char b = getchar();
        return 0;
}
view (30 lines, 100 lines of output)
pasted on Oct 7:
#include <stdio.h>

int main(int argc, char * argv[]) {
        int i, i_start = 1, i_end = 100;

        unsigned char flag = 0;
        for (i = i_start; i <= i_end; ++i) {
                flag = 0;
                if ((i % 3) == 0) {
                        flag += 1;
                }
                if ((i % 5) == 0) {
                        flag += 2;
                }
                if (flag) {
                        if (flag & 0x01) {
                                printf("Fizz");
                        }
                        if (flag & 0x02) {
                                printf("Buzz");
                        }
                }
                else {
                        printf("%d", i);
                }
                printf("\n");
        }
        char b = getchar();
        return 0;
}
view (30 lines, 100 lines of output)


C++:
pasted on Nov 12:
1
2
int main(void){for(int i=1;i<101;i++){if(i%3==0){cout<<"FIZZ";}if(i%5==0){cout<<"BUZZ";}if(i%3&&i%5){cout<<i;}cout<<endl;}}
          
view (2 lines, 100 lines of output)
pasted on Nov 12:
1
2
int main(void){for(int i=1; i<101; i++){if (i%3==0){cout<<"FIZZ";}if (i%5==0){cout<<"BUZZ";}if (i%3&&i%5){cout<<i;}cout<<endl;}}
          
view (2 lines, 100 lines of output)


Haskell:
pasted on Jun 21:
1
2
3
4
5
6
7
import Data.List
fizzBuzzList :: Int -> [String]
fizzBuzzList n = zipWith3(\i f b -> if f++b == "" then show i else f++b)
    [1..n] (cycle ["","","Fizz"]) (cycle [ "", "", "", "", "Buzz"])

main :: IO()
main = mapM_ putStrLn $ fizzBuzzList 100
view (7 lines, 100 lines of output)


PHP:
pasted on Sep 8:
1
2
3
4
5
6
7
8
9
10
11
<?php
function fizzbuzz($fizz = 3, $buzz = 5, $total = 100, $echo = true){
    $output = '';
    for($i = 1; $i <= $total; $i++) {
        $output .= ($i % $fizz == 0 && $i % $buzz == 0 ? "FizzBuzz\n" : ($i % $fizz == 0 ? "Fizz\n" : ($i % $buzz == 0 ? "Buzz\n" : ($echo ? $i."\n" : ''))));
    }
    return $output;
}

echo fizzbuzz();
?>
view (11 lines, 100 lines of output)
pasted on Aug 23:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
    $input=100;
    for($i=1; $i<=$input; $i++){
        if($i % 3 == 0 && $i % 5 ==0){
            print("FizzBuzz\n");
        } elseif($i % 3 == 0){
            print("Fizz\n");
        } elseif($i % 5 == 0){
            print("Buzz\n");
        } else {
            print($i."\n");
        }
    }
?>
view (14 lines, 100 lines of output)


Perl:
pasted on Oct 30:
1
2
3
4
5
6
7
@FB1 = (1..100);
@FB2 = map{!($_%3 or $_%5)?'FizzBuzz': $_}@FB1;
@FB3 = map{(/\d/ and !($_%3))?'Fizz':$_}@FB2;
@FB4 = map{(/\d/ and !($_%5))?'Buzz':$_}@FB3;
@FB5 = map{$_."\n"}@FB4;

print @FB5;
view (7 lines, 100 lines of output)
pasted on Oct 18:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(1..100){
    print "Fizz" if it_is_fizzy ($_);
    print "Buzz" if it_is_buzzy ($_);
    print "$_" if it_is_boring ($_);
    print "\n" # if it is anything at all
}

sub it_is_fizzy{
    !($_[0]%3)
}
sub it_is_buzzy{
    !($_[0]%5)
}
sub it_is_boring{
    $_[0]%3 and $_[0]%5
}
view (16 lines, 100 lines of output)


Python:
pasted on Nov 16:
1
2
3
4
5
6
7
8
9
10
purpose = {3:'fizz', 5:'buzz'}
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 (10 lines, 100 lines of output)
pasted on Nov 7:
1
2
for i in range(1,101):
 print("FizzBuzz"[i*i%3*4:8--i**4%5] or i)
view (2 lines, 100 lines of output)


Ruby:
pasted on Aug 27:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 def fizz_buzz(numbers)
    numbers.each do |number|
        div_by_3 = number % 3 == 0
        div_by_5 = number % 5 == 0

        if div_by_3 && div_by_5
            puts "FizzBuzz"
        elsif div_by_3 
                puts "Fizz"
            elsif div_by_5
                    puts "Buzz"
                else
                    puts number
            end
        end
end

numbers = (1..100).to_a
fizz_buzz(numbers)
view (19 lines, 100 lines of output)


Scheme:
pasted on Jun 29:
1
2
3
4
5
6
7
8
9
(define (fizzbuzz lower upper)
 (cond ((< upper lower) ())
          ((and (eq? (remainder lower 3) 0) (eq? (remainder lower 5) 0)) 
           (display "fizzbuzz\n") (fizzbuzz (+ lower 1) upper))
          ((eq? (remainder lower 3) 0) (display "fizz\n") (fizzbuzz (+ lower 1) upper))
          ((eq? (remainder lower 5) 0) (display "buzz\n") (fizzbuzz (+ lower 1) upper))
(else (display lower) (display "\n") (fizzbuzz (+ lower 1) upper))))

(fizzbuzz 1 100)
view (9 lines, 100 lines of output)
pasted on Jun 27:
1
2
3
4
5
6
7
8
9
(define (fizzbuzz lower upper)
 (cond ((< upper lower) ())
          ((and (eq? (remainder lower 3) 0) (eq? (remainder lower 5) 0)) 
           (display "fizzbuzz\n") (fizzbuzz (+ lower 1) upper))
          ((eq? (remainder lower 3) 0) (display "fizz\n") (fizzbuzz (+ lower 1) upper))
          ((eq? (remainder lower 5) 0) (display "buzz\n") (fizzbuzz (+ lower 1) upper))
(else (display lower) (display "\n") (fizzbuzz (+ lower 1) upper))))

(fizzbuzz 1 100)
view (9 lines, 100 lines of output)