[ create a new paste ] login | about

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

C:
htobing32@gmail.com - pasted on May 15:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>

main(){
    int i;
    int num = 100;

    for(i = 1; i <= 100; i++){
        if(i%3 == 0){
            printf("fizz");
        }
        if(i%5 == 0){
            printf("buzz");
        }
        if(i%3 != 0 && i%5 != 0){
            printf("%d", i);
        }
        printf("\n");        
    }

    return 0;
}
view (21 lines, 100 lines of output)
htobing32@gmail.com - pasted on May 15:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>

main(){
    int i;
    int num = 100;

    for(i = 1; i <= 100; i++){
        if(i%3 == 0){
            printf("fizz");
        }
        if(i%5 == 0){
            printf("buzz");
        }
        if(i%3 != 0 && i%5 != 0){
            printf("%d", i);
        }
        printf("\n");        
    }

    return 0;
}
view (21 lines, 100 lines of output)


C++:
pasted on May 23:
#include <cstdlib>
#include <stdio.h>

using namespace std;

void fizzBuzz() {
  // iterate over integers 1 to 100 inclusive.
  // print 1 line for each integer
  // if the integer is divisible by 3, print Fizz
  // if the integer is divisible by 5, print Buzz
  // if the integer is divisible by 15, print FizzBuzz
  // for all other integers, just print the number
  
  for (int i = 1; i <= 100; i++)
  {
      if (i % 15 == 0)
          cout << "FizzBuzz" << endl;
      else if (i % 3 == 0)
          cout << "Fizz" << endl;
      else if (i % 5 == 0)
          cout << "Buzz" << endl;
      else
          cout << i << endl;
  }
}


int main(int argc, const char** argv) {
  fizzBuzz();
  return 0;
}
view (31 lines, 100 lines of output)
pasted on May 5:
#include <cstdlib>
#include <stdio.h>

using namespace std;

void fizzBuzz() {
  // iterate over integers 1 to 100 inclusive.
  // print 1 line for each integer
  // if the integer is divisible by 3, print Fizz
  // if the integer is divisible by 5, print Buzz
  // if the integer is divisible by 15, print FizzBuzz
  // for all other integers, just print the number
  
  for (int i = 1; i <= 100; i++)
  {
      if (i % 15 == 0)
          cout << "FizzBuzz" << endl;
      else if (i % 3 == 0)
          cout << "Fizz" << endl;
      else if (i % 5 == 0)
          cout << "Buzz" << endl;
      else
          cout << i << endl;
  }
}


int main(int argc, const char** argv) {
  fizzBuzz();
  return 0;
}
view (31 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 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 Apr 30:
1
2
3
4
5
6
7
8
9
@a = ('',"Fizz","Buzz","FizzBuzz");


for(1..100){
    $a[0]=$_; 
    print $a[!($_%3) + !($_%5)*2];
    print "\n";
} 
    
view (9 lines, 100 lines of output)
pasted on Feb 12:
1
2
3
4
5
6
7
8
my $text;
for (1..100){
    $text .= 'Fizz' unless $_%3;
    $text .= 'Buzz' unless $_%5;
    $text? print $text : print $_;
    print "\n";
    $text = '';
}
view (8 lines, 100 lines of output)


Python:
pasted on May 20:
1
2
3
4
5
6
7
8
9
10
for n in xrange(1,101):

    if n % 5 ==0 and n % 3 == 0:
        print "Fizzbuzz"
    elif n % 3 == 0:
        print "Fizz"
    elif n % 5 == 0:
        print "Buzz"
    else:
        print n
view (10 lines, 100 lines of output)
pasted on Feb 14:
1
2
3
4
5
6
7
8
9
10
for n in xrange(1,101):

    if n % 5 ==0 and n % 3 == 0:
        print "Fizzbuzz"
    elif n % 3 == 0:
        print "Fizz"
    elif n % 5 == 0:
        print "Buzz"
    else:
        print n
view (10 lines, 100 lines of output)


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