[ create a new paste ] login | about

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

C:
pasted on May 25:
#include <stdio.h>
#include <stdlib.h>

void doFizzBuzz( unsigned i ) {
	const char *const fizz = i%3 == 0 ? "fizz" : "";
	const char *const buzz = i%5 == 0 ? "buzz" : "";

	if( !( *fizz | *buzz ) ) {
		printf( "%u\n", i );
	} else {
		printf( "%s%s\n", fizz, buzz );
	}
}

int main() {
        unsigned i;

	for( i = 1; i <= 100; i += 1 ) {
		doFizzBuzz( i );
	}

	return EXIT_SUCCESS;
}
view (23 lines, 100 lines of output)
pasted on May 22:
#include <stdio.h>
#include <stdlib.h>

void doFizzBuzz( unsigned i ) {
	const char *const fizz = i%3 == 0 ? "fizz" : "";
	const char *const buzz = i%5 == 0 ? "buzz" : "";

	if( !( *fizz | *buzz ) ) {
		printf( "%u\n", i );
	} else {
		printf( "%s%s\n", fizz, buzz );
	}
}

int main() {
        unsigned i;

	for( i = 1; i <= 100; i += 1 ) {
		doFizzBuzz( i );
	}

	return EXIT_SUCCESS;
}
view (23 lines, 100 lines of output)


C++:
pasted on Jun 15:
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 (14 lines, 100 lines of output)
pasted on Jun 15:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
   for (int i = 1; i < 101; ++i)
   {
       if      (i % 3 == 0 && i % 5 == 0) cout << "FizzBuzz";
       else if (i % 3 == 0)               cout << "Fizz";
       else if (i % 5 == 0)               cout << "Buzz";
       else                               cout << i;
       cout << endl;
   }
}
view (15 lines, 100 lines of output)


Haskell:
pasted 5 hours ago:
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 Apr 24:
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)
pasted on Apr 24:
1
2
3
4
5
6
7
8
9
<?php
  for($i=1;$i<=100;$i++){
    $item=$i%3===0?"Fizz":"";
    $item=$i%5===0?$item."Buzz":$item;
    $item=$item===""?$i:$item;
    $item.="\n";
    echo $item;
  }
?>
view (9 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 Mar 29:
1
2
3
4
5
6
7
8
9
def fbr(n):
    return (
        ("" if n%3 else "fizz") +
        ("" if n%5 else "buzz")
    ) or str(n)

if __name__ == "__main__":
    for i in range(100):
        print fbr(i+1)
view (9 lines, 100 lines of output)
pasted on Mar 16:
#! /usr/bin/env python3
# fizzbuzz.py

def divisor_response(divisor, response):
    # say fizz if the number is divisible by 3
    return lambda n: "" if n % divisor != 0 else response

def arbitrary_divisor_responses(d_responses):
    # combine fizzbuzz responses in a specific order
    # or say the number if none of them apply
    return lambda n: "".join(
        [divisor_response(*s)(n) for s in d_responses] ) or str(n)

def fizzbuzz(n):
    # actual fizzbuzz
    return arbitrary_divisor_responses([
        (3, "fizz"), (5, "buzz")])(n)

def fb_start_end(start, end):
    # fizzbuzz within a range
    return "\n".join([
        fizzbuzz(n) for n in range(start, end+1)])

if __name__ == "__main__":
    print(fb_start_end(1, 100))
view (26 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)
pasted on Feb 7:
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 (11 lines, 100 lines of output)