[ create a new paste ] login | about

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

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

typedef enum { false, true } bool;

int main(void)
{
    int i = 0;
    while (++i <= 100) {
        const bool fizz = i % 3 == 0, buzz = i % 5 == 0;

        if (fizz || buzz) {
            if (fizz) printf("Fizz");
            if (buzz) printf("Buzz");
            putchar('\n');
        }
        else printf("%d\n", i);
    }

    return EXIT_SUCCESS;
}
view (21 lines, 100 lines of output)
pasted on Jun 14:
#include <stdio.h>

int main(void){

    int i;

    printf("1\n2\n");

    for(i=3; i<=100; i++){

        if(i%15==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 (32 lines, 100 lines of output)


C++:
pasted on Jun 21:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <sstream>

int main()
{
for(int i=0;i<100;i++)
{
    std::stringstream ss;
    if((i+1) % 3 == 0) ss<<"Fizz";
    if((i+1) % 5 == 0) ss<<"Buzz";
    if((int)strlen(ss.str().c_str()) == 0) ss<<i+1;
    std::cout<<ss.str()<<std::endl;
}
return 0;
}
view (16 lines, 100 lines of output)
pasted on Jun 21:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <sstream>

int main()
{
for(int i=0;i<100;i++)
{
    std::stringstream ss;
    
    if((i+1) % 3 == 0) ss<<"Fizz";
    if((i+1) % 5 == 0) ss<<"Buzz";
    if((int)strlen(ss.str().c_str()) == 0)
    ss<<i+1;
    std::cout<<ss.str()<<std::endl;
}
return 0;
}
view (18 lines, 100 lines of output)


Haskell:
pasted on May 13:
1
2
3
4
5
6
7
8
9
10
fizzBuzz :: (Integral a) => a -> String
fizzBuzz i
  | fizz && buzz = "FizzBuzz"
  | fizz = "Fizz"
  | buzz = "Buzz"
  | otherwise = show i
  where fizz = mod i 3 == 0
        buzz = mod i 5 == 0

main = mapM_ (putStrLn . fizzBuzz) [1..100]
view (10 lines, 100 lines of output)
alikhosroshahi81 - pasted on May 11:
1
2
3
4
5
6
7
8
9
10
fizzBuzz :: (Integral a) => a -> String
fizzBuzz i
  | fizz && buzz = "FizzBuzz"
  | fizz = "Fizz"
  | buzz = "Buzz"
  | otherwise = show i
  where fizz = mod i 3 == 0
        buzz = mod i 5 == 0

main = mapM_ (putStrLn . fizzBuzz) [1..100]
view (10 lines, 100 lines of output)


Lua:
pasted on Apr 12:
1
2
3
4
5
6
7
8
9
10
11
for i = 1, 100 do
   if i % 3 == 0 and i % 5 == 0 then
      print "FizzBuzz"
   elseif i % 3 == 0 then
      print "Fizz"
   elseif i % 5 == 0 then
      print "Buzz"
   else
      print(i)
   end
end
view (11 lines, 100 lines of output)


PHP:
pasted on Jun 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
for($i=1; $i<=100; $i++) {
    $res= '';
    if(($i%3)===0){
        $res = 'Fizz';		
    }
    if(($i%5)===0){
        $res .= 'Buzz';
    }
    if($res === '') {
	$res = $i;
    }
    echo $res."\n";
}
?>
view (15 lines, 100 lines of output)
pasted on Jun 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
for ($i = 1; $i <= 100; $i++)
{
    if($i % 3 == 0 && $i % 5 ==0){
        echo "FizzBuzz\n";
    }
    else if($i % 3 == 0){
        echo "Fizz\n";
    }
    else if($i % 5 == 0){
        echo "Buzz\n";
    }
    else {
        echo $i."\n";
    }
}
?>
view (17 lines, 100 lines of output)


Perl:
pasted on Jun 13:
1
2
3
4
5
6
7
for my $a (1..100){
    
    $a .= 'fizz' unless $a%3;
    $a .= 'buzz' unless $a%5;
    $a =~s/\d+([a-z])/$1/;
    print "$a\n";
}
view (7 lines, 100 lines of output)
pasted on May 26:
1
for(1..100){($_%3?0:print "Fizz")+($_%5?0:print "Buzz")?0:print $_;print "\n"}
view (1 line, 100 lines of output)


Python:
pasted on Jun 24:
1
2
3
4
5
6
7
8
9
for i in range(1, 101):
    if i % 15 == 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 Jun 24:
1
2
3
4
5
6
7
8
9
for i in range(1, 101):
    if i % 15 == 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 Jun 27:
1
2
3
4
5
6
7
8
9
10
11
for i in 1..100
   if i % 15 == 0
      puts "FizzBuzz"
   elsif i % 3 == 0
      puts "Fizz"
   elsif i % 5 == 0
      puts "Buzz"
   else
      puts i
   end
end
view (11 lines, 100 lines of output)
pasted on Jun 5:
1
2
3
4
5
puts (1..100).map {|i|
 	f = i % 3 == 0 ? 'Fizz' : nil
 	b = i % 5 == 0 ? 'Buzz' : nil
 	f || b ? "#{f}#{b}" : i
 }
view (5 lines, 100 lines of output)