[ create a new paste ] login | about

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

C:
pasted on Aug 25:
1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
//#include<stdlib.h>

int main(void){
  int i = 1;
  while (i <= 100){
  printf("%d\n\0Fizz\n\0FizzBuzz\n"+!(i%3)*4+!(i%5)*14-!(i%15)*8, i);
  i++;
  }
return 0;
}
view (11 lines, 100 lines of output)
pasted on Aug 24:
1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
//#include<stdlib.h>

int main(void){
  int i = 1;
  while (i <= 100){
  printf("%d\n\0Fizz\n\0FizzBuzz\n"+!(i%3)*4+!(i%5)*14-!(i%15)*8, i);
  i++;
  }
return 0;
}
view (11 lines, 100 lines of output)


C++:
pasted yesterday:
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 Jul 13:
1
2
3
4
5
6
7
8
9
10
#include "stdio.h"
int main(void) {
    int x;
    for(x=1;x<101;x++) {
        if (x%3==0) printf("Fizz");
        if (x%5==0) printf("Buzz");
        else if (x%3) printf("%d", x);
        printf("\n");
    }
}
view (10 lines, 100 lines of output)


Haskell:
pasted on Jul 15:
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)
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)


Lua:
pasted on Aug 2:
1
2
3
4
5
6
7
8
9
10
11
for i=1,100 do
    if math.mod(i,3)==0 and math.mod(i,5)==0 then
    print('fizzbuzz')
    elseif math.mod(i,5)==0 then
    print('buzz')
    elseif math.mod(i,3)==0 then
    print('fizz')
    else
    print(i)
    end
end
view (11 lines, 100 lines of output)
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 Aug 25:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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 (14 lines, 100 lines of output)
pasted on Jul 20:
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)


Perl:
pasted on Jul 15:
1
for (1 .. 100) { print $_ % 15? $_ % 5 ? $_ % 3 ? $_ . "\n" : "Fizz\n" : "Buzz\n" : "FizzBuzz\n";  }
view (1 line, 100 lines of output)
pasted on Jul 13:
1
for (1 .. 100) { print $_ % 15? $_ % 5 ? $_ % 3 ? $_ . "\n" : "Fizz\n" : "Buzz\n" : "FizzBuzz\n";  }
view (1 line, 100 lines of output)


Python:
Woofless77 - pasted on Aug 18:
1
for i in range(1,101):print(i%3<1)*"Fizz"+(i%5<1)*"Buzz"or i
view (1 line, 100 lines of output)
pasted on Jul 20:
1
for i in range(1,101):print(i%3<1)*"Fizz"+(i%5<1)*"Buzz"or i
view (1 line, 100 lines of output)


Ruby:
pasted yesterday:
1
2
3
4
5
100.times do |i| # do this for the first 100 numbers
    n = i+1; fizzbuzz = false;
    if (n%3==0 || n%5==0) then fizzbuzz = true end
    puts (fizzbuzz ? (n%3==0 ? "Fizz" : "") + (n%5==0 ? "Buzz" : "") : n)
end
view (5 lines, 100 lines of output)
pasted on Aug 25:
1
2
3
4
5
6
(1..100).each do |i|
    msg = ""
    msg += "Fizz" if i % 3 == 0
    msg += "Buzz" if i % 5 == 0
    puts (msg == "") ? i : msg
end
view (6 lines, 100 lines of output)