[ create a new paste ] login | about

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

C:
pasted on Aug 10:
#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 9:
#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 Aug 8:
1
2
3
4
5
6
7
8
9
10
11
12
13
using namespace std;

int main()
{
   for (int num = 1; num < 101; ++num)
   {
       if      (num % 3 == 0 && num % 5 == 0) cout << "FizzBuzz";
       else if (num % 3 == 0)               cout << "Fizz";
       else if (num % 5 == 0)               cout << "Buzz";
       else                               cout << num;
       cout << endl;
   }
}
view (13 lines, 100 lines of output)
pasted on Aug 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main (){
    for (int i = 1; i <= 100; i++){
    if (i % 3 == 0 && i % 5 == 0) cout << "FizzBuzz\n";
    else if (i % 3 == 0) cout << "Fizz\n";
    else if (i % 5 == 0) cout << "Buzz\n";
    else cout << i << '\n';
}

    return 0;
}
view (13 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 Aug 6:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

for ($i =1; $i < 101; $i++){

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

echo "\n";
}

?>
view (18 lines, 100 lines of output)
pasted on Jul 18:
1
2
3
4
5
6
7
8
9
10
<?php
  $input=100;
  for($i=1;$i<=$input;$i++){
    $item=$i%3===0?"Fizz":"";
    $item=$i%5===0?$item."Buzz":$item;
    $item=$item===""?$i:$item;
    $item.="\n";
    echo $item;
  }
?>
view (10 lines, 100 lines of output)


Perl:
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)
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 Aug 10:
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)
pasted on Jul 19:
1
2
3
4
5
6
7
8
9
for num in range(1, 101):
  if num % 15 == 0:
    print("FizzBuzz")
  elif num % 3 == 0:
    print("Fizz")
  elif num % 5 == 0:
    print("Buzz")
  else:
    print(num)
view (9 lines, 100 lines of output)


Ruby:
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)