[ create a new paste ] login | about

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

C:
pasted on Jul 9:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>

int main(void) {
	int i;

	for (i = 1; i <= 100; i++)

		if (i % 15 == 0)
			printf("FizzBuzz\n", i);
		else if (i % 3 == 0)
			printf("Fizz\n", i);
		else if (i % 5 == 0)
			printf("Buzz\n", i);
		else
			printf("%d\n", i);
			
	return 0;
}
view (18 lines, 100 lines of output)
pasted on Jul 8:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>

int main(void) {
	int i;

	for (i = 1; i <= 100; i++)

		if (i % 15 == 0)
			printf("FizzBuzz\n", i);
		else if (i % 3 == 0)
			printf("Fizz\n", i);
		else if (i % 5 == 0)
			printf("Buzz\n", i);
		else
			printf("%d\n", i);
			
	return 0;
}
view (18 lines, 100 lines of output)


C++:
pasted on Jun 9:
#include <iostream>

using namespace std;

void fizzbuzz1()
{
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		string output = "";
		output += ( ch % 3 == 0 ) ? "fizz":"";
		output += ( ch % 5 == 0 ) ? "buzz":"";
		
		if( output.size() == 0 )
		{
			cout << +ch;
		}
		else
		{
			cout << output;
		}
		cout <<  "\n";
	}
}

void fizzbuzz2()
{
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		bool flg = false;
		if( ch % 3 == 0 )
		{
			cout << "fizz";
			flg = true;
		}
		if( ch % 5 == 0 )
		{
			cout << "buzz";
			flg = true;
		}
		
		if( !flg )
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

void fizzbuzz3()
{
	const char* outputs[]  = { "", "fizz", "buzz", "fizzbuzz" };
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		unsigned char index = 0;
		if( ch % 3 == 0 )
		{
			index++;
		}
		if( ch % 5 == 0 )
		{
			index+=2;
		}
		
		if( index > 0 )
		{
			cout << outputs[index];
		}
		else
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

void fizzbuzz4()
{
	const char* outputs[]  = { "", "fizz", "buzz", "fizzbuzz" };
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		unsigned char index = 0;
		index += ( ch % 3 == 0 );
		index += ( ch % 5 == 0 )*2;

		if( index > 0 )
		{
			cout << outputs[index];
		}
		else
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

int main()
{
     fizzbuzz4();
    
    return 0;
}
view (102 lines, 100 lines of output)
pasted on Jun 9:
#include <iostream>

using namespace std;

void fizzbuzz1()
{
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		string output = "";
		output += ( ch % 3 == 0 ) ? "fizz":"";
		output += ( ch % 5 == 0 ) ? "buzz":"";
		
		if( output.size() == 0 )
		{
			cout << +ch;
		}
		else
		{
			cout << output;
		}
		cout <<  "\n";
	}
}

void fizzbuzz2()
{
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		bool flg = false;
		if( ch % 3 == 0 )
		{
			cout << "fizz";
			flg = true;
		}
		if( ch % 5 == 0 )
		{
			cout << "buzz";
			flg = true;
		}
		
		if( !flg )
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

void fizzbuzz3()
{
	const char* outputs[]  = { "", "fizz", "buzz", "fizzbuzz" };
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		unsigned char index = 0;
		if( ch % 3 == 0 )
		{
			index++;
		}
		if( ch % 5 == 0 )
		{
			index+=2;
		}
		
		if( index > 0 )
		{
			cout << outputs[index];
		}
		else
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

void fizzbuzz4()
{
	const char* outputs[]  = { "", "fizz", "buzz", "fizzbuzz" };
	for( unsigned char ch = 1; ch < 101; ch++ )
	{
		unsigned char index = 0;
		index += ( ch % 3 == 0 );
		index += ( ch % 5 == 0 );

		if( index > 0 )
		{
			cout << outputs[index];
		}
		else
		{
			cout << +ch;
		}
		cout << "\n";
	}
}

int main()
{
     fizzbuzz3();
    
    return 0;
}
view (102 lines, 100 lines of output)


PHP:
pasted on Apr 22:
1
2
3
4
5
6
7
8
<?php

for($i=1;$i<=100;$i++){
if ($i%3==0) echo "Fizz";
if ($i%5==0) echo "Buzz";
if ($i%3 >0 && $i%5 > 0) echo $i;
echo "\n";
}
view (8 lines, 100 lines of output)
pasted on Mar 24:
<?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)


Perl:
pasted on Jul 19:
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 Jul 19:
1
2
3
4
5
6
7
for (1..100){
    my$text;
    $text .= 'Fizz' unless $_%3;
    $text .= 'Buzz' unless $_%5;
    $text = $_ unless $text;
    print "$text\n";
}
view (7 lines, 100 lines of output)


Python:
pasted on Jul 19:
1
2
for i in range(1,101) :
    print("Fizz"*(i%3<1)+(i%5<1)*"Buzz"or i)
view (2 lines, 100 lines of output)
pasted on Jul 19:
1
2
for i in range(1,101) :
    print("Fizz"*(i%3<1)+(i%5<1)*"Buzz"or i)
view (2 lines, 100 lines of output)


Ruby:
pasted on Jul 12:
1
2
3
4
5
6
7
8
9
10
11
12
def fizz_buzz(numbers)
    
    numbers.each do |number|
       response = ""
       response += "Fizz" if number % 3 == 0
       response += "Buzz" if number % 5 == 0
       puts response.empty? ? number : response
    end
end

numbers = (1..100).to_a
fizz_buzz(numbers)
view (12 lines, 100 lines of output)
pasted on Feb 7:
1
2
3
4
5
6
7
8
9
10
11
12
def fizz_buzz(numbers)
    
    numbers.each do |number|
       response = ""
       response += "Fizz" if number % 3 == 0
       response += "Buzz" if number % 5 == 0
       puts response.empty? ? number : response
    end
end

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