[ create a new paste ] login | about

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

C:
pasted on Jan 10:
#include<stdio.h>

int func(int x)
{
    if((x % 3 == 0)&&(x % 5 == 0)){
        printf("FizzBuzz\n");
    }
    else if(x % 3 == 0){
        printf("Fizz\n");
    }
    else if(x % 5 == 0){
        printf("Buzz\n");
    }
    else{
        printf("%d\n", x);
    }
}

void main(void){
    int i;
    for(i = 1; i <= 100; i++){
        func(i);
    }
}
view (24 lines, 100 lines of output)
pasted on Jan 4:
#include<stdio.h>

int func(int x)
{
    if((x % 3 == 0)&&(x % 5 == 0)){
        printf("FizzBuzz\n");
    }
    else if(x % 3 == 0){
        printf("Fizz\n");
    }
    else if(x % 5 == 0){
        printf("Buzz\n");
    }
    else{
        printf("%d\n", x);
    }
}

void main(void){
    int i;
    for(i = 1; i <= 100; i++){
        func(i);
    }
}
view (24 lines, 100 lines of output)


C++:
pasted on Dec 13:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;

void f1() {
  for (int i = 1; i <= 100; i++) {
    if(i % 3 == 0) cout << "fizz";
    if(i % 5 == 0) cout << "buzz";
    if((i % 3) && (i % 5)) cout << i;
    cout << endl;
  }
}

int main() {
  f1();
}
view (15 lines, 100 lines of output)
pasted on Dec 5:
#include<iostream>
using namespace std;

void f1(){

int i;
for (i=1;i<=100;i++){
if(i%3==0){
cout<<"fizz";
}//if

if(i%5==0){
cout<<"buzz";
}//if

if( (i%3!=0) && (i%5!=0)){
cout<<i;
}//if

cout<<"\n";
}//for

}//void

int main(){

f1();

}
view (29 lines, 100 lines of output)


Haskell:
pasted on Jan 16:
1
2
3
4
5
6
7
8
9
import Data.List

main :: IO ()
main = mapM_ putStrLn $ take 100 fizzbuzz

fizzbuzz :: [String]
fizzbuzz = zipWith3 (\n fizz buzz ->
                     if null (fizz++buzz) then show (n::Int) else fizz++buzz)
             [1..] (cycle ["","","Fizz"]) (cycle ["","","","","Buzz"])
view (9 lines, 100 lines of output)
pasted on Nov 27:
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 Jan 14:
<?php
for ($i = 1; $i <= 100; $i++)
{
	$output = '';

	if ($i % 3 == 0)
	{
		$output .= 'Fizz';
	}

	if ($i % 5 == 0)
	{
		$output .= 'Buzz';
	}

	if (!$output)
	{
		$output = $i;
	}

	echo $output . "\n";
}
?>
view (23 lines, 100 lines of output)
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)


Perl:
pasted on Nov 28:
# FizzBuzz by code reference. (Without decision logic!!!)



# First, take a fresh array. Be sure to wash it thoroughly to remove any dirt and residue.
# Next, add in four code references, like so:
    @a=(
        sub {print "FizzBuzz\n"},
        sub {print "Buzz\n"},
        sub {print "Fizz\n"},
        sub {print "$_[0]\n"}
    );

# Set the array aside. 


# Next, take a list of numbers (from 1 to 100)
# Then, take two "<=>" (spaceship operators) and two 0s
# Combine them with each test value to produce two new values (1 or 0 for each new value).
#     The two values are the "multiple of 3 test result" and the "multiple of 5 test result."
# Spice up the "multiple of 5 test result" by multiplying it by 2.

#        Mmmmm. . . Spicy!


# Combine the fresh test results to come up with a new value that is
# specific to the properties of the original value.

# A value that is a multiple of 3 and 5 will get a combined test value result of 0.
# A value that is a multiple of 5 will get a combined test value result of 1.
# A value that is a multiple of 3 will get a combined test value result of 2.
# A value that is not a multiple of 3 and 5 will get a combined test value result of 3.


# Take the piping hot test value result and use it as an index for the array of code references.
# Be sure to pass the original value through the array for the times when the code reference 
# at $a[3] is called!!!

    $a[($_%3<=>0) + (2*($_%5<=>0))]->($_) for (1..100)

# And, would you look at that!? Each code reference prints out
# "FizBuzz," "Buzz," "Fizz," or the original value 
# based on that crunchy-on-the-outside, tender-in-the-middle 
# combined test result!
view (46 lines, 100 lines of output)
pasted on Nov 28:
# FizzBuzz by transforming lists. 

# A list that goes from 1 to 100
    @FB1 = (1..100);

# Make a new list from the previous list. 
# The new list replaces all values that are multiples of 3 & 5 with "FizzBuzz"
    @FB2 = map{!($_%3 or $_%5)?'FizzBuzz': $_}@FB1;

# Make a new list from the previous list.
# The new list replaces all values that are multiples of 3 with "Fizz"
    @FB3 = map{(/\d/ and !($_%3))?'Fizz': $_}@FB2;

# Make a new list from the previous list.
# The new list replaces all values that are multiples of 5 with "Buzz"
    @FB4 = map{(/\d/ and !($_%5))?'Buzz': $_}@FB3;

# Make a new list from the previous list.
# Add a line return to each value in that list
    @FB5 = map{$_."\n"}@FB4;

# print the final list and toast your success with some bubbly.
print @FB5;
view (23 lines, 100 lines of output)


Python:
pasted on Jan 16:
1
2
3
4
5
6
7
8
9
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 (9 lines, 100 lines of output)
pasted on Dec 19:
1
2
3
4
5
6
7
8
9
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 (9 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)