[ create a new paste ] login | about

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

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

int main() {
  int i;
  for (i = 1; i < 101; i++) {

    int div3 = 0 == i % 3;
    int div5 = 0 == i % 5;

    if (div3 || div5) {
      printf("%s%s\n", 
        div3 ? "fizz" : "", 
        div5 ? "buzz" : "");
    } else {
      printf("%d\n", i);
    }
  }
  return 0;
}
view (19 lines, 100 lines of output)
pasted on Dec 12:
#include<stdio.h>
#include<math.h>

int main()
{
	int i;
	for(i=1;i<101;i++)
	{
		
		if ( (( i % 3)==0)  && ((i % 5)== 0))
		{
	            printf("FizzBuzz\n");
                }
                else
                {
	            if (( i % 3)==0)
                    {
	                printf("Fizz\n");
                    }
                    else
                    {
	                if ((i % 5)== 0) 
	                {
	                    printf("Buzz\n");
                        }
                        else
                        {
                            printf("%d\n",i);
                        }
                    }        
                }
		
        }

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


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


Haskell:
pasted on Jan 21:
1
2
3
4
5
6
7
8
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 (8 lines, 100 lines of output)
pasted on Jan 21:
1
2
3
4
5
6
7
8
newtype FizzBuzz = MakeFizzBuzz Int

instance Show FizzBuzz where
  show (MakeFizzBuzz x) = if str == "" then show x else str
    where str = (if x `mod` 3 == 0 then "Fizz" else "") ++
                  (if x `mod` 5 == 0 then "Buzz" else "")

main = mapM_ print [MakeFizzBuzz x | x <- [1..100]]
view (8 lines, 100 lines of output)


Lua:
pasted on Dec 20:
1
2
3
4
5
6
7
for i=1, 100 do
    output = ''
    if i%3 == 0 then output = 'Fizz' end
    if i%5 == 0 then output = output..'Buzz' end
    if output == '' then output = i end
    print(output)
end
view (7 lines, 100 lines of output)
pasted on Dec 20:
1
2
3
4
5
6
7
for i=1, 100 do
    output = ''
    if i%3 == 0 then output = 'Fizz' end
    if i%5 == 0 then output = output..'Buzz' end
    if output == '' then output = i end
    print(output)
end
view (7 lines, 100 lines of output)


PHP:
pasted on Dec 6:
1
2
3
4
5
6
7
8
9
<?php
for($i=1;$i<=100;$i++){
$str = '';
if($i%3==0) { $str .= 'Fizz'; }
if($i%5==0) { $str .= 'Buzz'; }
if($str=='') { echo $i; } else { echo $str; }
echo "\n";
}
?>
view (9 lines, 100 lines of output)
pasted on Oct 14:
1
2
3
4
5
6
7
8
9
<?php
for($i=1;$i<=100;$i++){
$str = '';
if($i%3==0) { $str .= 'Fizz'; }
if($i%5==0) { $str .= 'Buzz'; }
if($str=='') { echo $i; } else { echo $str; }
echo "\n";
}
?>
view (9 lines, 100 lines of output)


Perl:
pasted on Feb 1:
1
2
@a = ('Spiffy!','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 Feb 3:
1
for x in range(1,101): print('Fizz'*(x%3 == 0) + 'Buzz'*(x%5 == 0) or x)
view (1 line, 100 lines of output)
pasted on Jan 17:
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 ==00:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)
view (9 lines, 100 lines of output)


Ruby:
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)
pasted on Feb 6:
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)