[ create a new paste ] login | about

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

C:
pasted on Sep 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 Aug 30:
#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 Sep 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main() {
    for(int i = 1; i < 101; i++) {
        if(i % 15 == 0) {
            std::cout << "FizzBuzz" << std::endl;
        } else if (i % 5 == 0) {
            std::cout << "Buzz" << std::endl;
        } else if (i % 3 == 0) {
            std::cout << "Fizz" << std::endl;
        } else {
            std::cout << i << std::endl;
        }
    }

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

int main() {
    for(int i = 1; i < 101; i++) {
        if(i % 15 == 0) {
            std::cout << "FizzBuzz" << std::endl;
        } else if (i % 5 == 0) {
            std::cout << "Buzz" << std::endl;
        } else if (i % 3 == 0) {
            std::cout << "Fizz" << std::endl;
        } else {
            std::cout << i << std::endl;
        }
    }

    return 0;
}
view (17 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)
pasted on May 15:
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 Sep 7:
1
2
3
4
5
6
7
for (1..100){
   my$print;
   $print.='Fizz' unless $_%3;
   $print.='Buzz' unless $_%5;
   $print=$_ unless $print;
   print "$print\n"
}
view (7 lines, 100 lines of output)
pasted on Apr 4:
1
2
@a = ('','Fizz','Buzz','FizzBuzz');
print join "\n", map{ $a[0] = $_;  $a[(!($_%3) + !( $_%5)*2)]  }(1..100);
view (2 lines, 100 lines of output)


Python:
pasted on Sep 14:
1
2
3
4
5
6
7
8
9
10
11
N = 100

for i in range(1, N + 1):
    to_print = ''
    if i % 3 == 0:
        to_print += 'Fizz'
    if i % 5 == 0:
        to_print += 'Buzz'
    if len(to_print) < 1:
        to_print = str(i)
    print(to_print)
view (11 lines, 100 lines of output)
ashlerfox - pasted on Sep 9:
1
2
3
4
5
6
for i in range(1,101):
    s = ""
    if not i % 3: s += "Fizz"
    if not i % 5: s += "Buzz"
    if not s: s = i
    print s
view (6 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)
pasted on May 17:
1
2
3
4
5
6
7
8
9
10
11
for num in 1..100 do
 if num % 15 == 0
   puts "FizzBuzz"
 elsif num % 3 == 0
   puts "Fizz"
 elsif num % 5 == 0
   puts "Buzz"
 else
   puts num
 end
end
view (12 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)