[ create a new paste ] login | about

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

C:
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)
pasted on Dec 9:
#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 on Dec 24:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
    for (int i = 1; i < 101; i++) {
        bool fizz = i%3 == 0, buzz = i%5 == 0;
        if (fizz)
            cout << "Fizz";
            
        if (buzz) 
            cout << "Buzz";
            
        if (!(fizz || buzz))
            cout << i;
            
        cout << '\n';
    }
}
view (20 lines, 100 lines of output)
pasted on Dec 20:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
    for (int i = 1; i < 101; i++) {
        bool fizz = i%3 == 0, buzz = i%5 == 0;
        if (fizz)
            cout << "Fizz";
            
        if (buzz) 
            cout << "Buzz";
            
        if (!(fizz || buzz))
            cout << i;
            
        cout << '\n';
    }
}
view (20 lines, 100 lines of output)


Haskell:
pasted yesterday:
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 yesterday:
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 Aug 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (1..100){
    my $f = $_ % 3;
    my $b = $_ % 5;
    if( !$f ){
        print "fizz";
    }
    if( !$b ){
        print "buzz"
    }
    if( $f && $b ){
        print $_;
    }
    print "\n";
}
view (14 lines, 100 lines of output)


Python:
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)
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 Aug 15:
1
2
3
4
5
6
1.upto(100) do |i|
  out = nil
  out = out.to_s + 'Fizz' if i % 3 == 0
  out = out.to_s + 'Buzz' if i % 5 == 0
  puts out || i
end
view (6 lines, 100 lines of output)
pasted on Aug 15:
1
2
3
4
5
6
1.upto(100) do |i|
  out = nil
  out = out.to_s + 'Fizz' if i % 3 == 0
  out = out.to_s + 'Buzz' if i % 5 == 0
  puts out || i
end
view (6 lines, 100 lines of output)