[ create a new paste ] login | about

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

C:
pasted yesterday:
#include<stdio.h>
int main(void)
{
	int i;
	for(i=1;i<=100;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 (25 lines, 100 lines of output)
pasted on Apr 18:
#include<stdio.h>
int main(void)
{
	int i;
	for(i=1;i<=100;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 (25 lines, 100 lines of output)


C++:
pasted on Apr 18:
#include <iostream>
using namespace std;

int main()
{

    int num = 1; //プリントする数を格納する変数

    int multi_3 = 3; //3の倍数
    int multi_5 = 5; //5の倍数


    //1から100まで繰り返す
    for( num = 1; num <= 100; num++)
    {
        //3と5の両方の倍数のときFizzBuzzを出力
        if( num % multi_3 == 0 && num % multi_5 == 0)
        {
            printf("FizzBuzz\n");
        }
        
        //3の倍数のときFizzを出力
        else if( num % multi_3 == 0 )
        {
            printf("Fizz\n");
        }

        //5の倍数のときBuzzを出力
        else if( num % multi_5 == 0 )
        {
            printf("Buzz\n");
        }

        //numを出力する
        else
        {
            printf("%d\n", num);
        }

    }

    return 0;
}
view (43 lines, 100 lines of output)
pasted on Feb 25:
// Example program
#include <iostream>
#include <string>

int main()
{
 // std::string name;
  //std::cout << "What is your name? ";
  //getline (std::cin, name);
  //std::cout << "Hello, " << name << "!\n";
  
  int summ = 0;
  //double x;
  
  //std::cout << summ << std::endl;
  
  for(int i = 1; i <= 100; i++) {        
       
        if((i%3 != 0) && (i%5 != 0) )    std::cout << i ;
        
        if(i % 3 == 0)  std::cout << "Fizz";
        
        if(i % 5 == 0)  std::cout << "Buzz";
        
        std::cout << std::endl;
       
        
        
    }
    
    //std::cout << summ << std::endl;        
  
  
}
view (34 lines, 100 lines of output)


Haskell:
pasted on Mar 7:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fzzbzz :: Int -> String -> (String, Int) -> (String, Int)
fzzbzz m sound (str, x) = (str ++ if mod x m == 0 then sound else "", x)

fix :: (String, Int) -> String
fix ("", x) = show x
fix (st, _) = st

fizzbuzz :: [(String, Int)] -> Int -> String
fizzbuzz sounds top
    | top <= 0  = ""
    | otherwise = foldr (\x y -> x ++ "\n" ++ y) [] (map fizzer numbers)
    where fizzer = (foldl (.) fix (reverse [fzzbzz i s | (s, i) <- sounds]))
          numbers = take top (zip (cycle [""]) [1..])

main = putStr (fizzbuzz [("Fizz", 3), ("Buzz", 5)] 100)
view (15 lines, 100 lines of output)
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)


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 Mar 28:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$count = 1;
while ($count <= 100) {
    $word = "";
    if ($count % 3 == 0) {
        $word = "fizz";
    }
    if ($count % 5 == 0) {
        $word .= "buzz";
    }

    if(empty($word)) {
        $word = $count;
    }
    echo $word;
    echo PHP_EOL;
    $count++;
}
view (18 lines, 100 lines of output)
pasted on Mar 12:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
for ($i = 1; $i <= 100; $i++) {
    $is_fizz = ($i % 3 === 0);
    $is_buzz = ($i % 5 === 0);

    if ($is_fizz || $is_buzz) {
        if ($is_fizz) echo "Fizz";
        if ($is_buzz) echo "Buzz";
    } else {
        echo $i;
    }
    echo "\n";
}
view (13 lines, 100 lines of output)


Perl:
pasted on Apr 4:
1
2
@a = ('','Fizz','Buzz','FizzBuzz');
print join "\n", map{ $a[0] = $_;  $a[(!($_%3) + !( $_%5)*2)]  }(1..100);
view (2 lines, 100 lines of output)
pasted on Apr 4:
1
2
@a = ('','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 Mar 29:
1
2
3
4
5
6
7
8
9
def fbr(n):
    return (
        ("" if n%3 else "fizz") +
        ("" if n%5 else "buzz")
    ) or str(n)

if __name__ == "__main__":
    for i in range(100):
        print fbr(i+1)
view (9 lines, 100 lines of output)
pasted on Mar 16:
#! /usr/bin/env python3
# fizzbuzz.py

def divisor_response(divisor, response):
    # say fizz if the number is divisible by 3
    return lambda n: "" if n % divisor != 0 else response

def arbitrary_divisor_responses(d_responses):
    # combine fizzbuzz responses in a specific order
    # or say the number if none of them apply
    return lambda n: "".join(
        [divisor_response(*s)(n) for s in d_responses] ) or str(n)

def fizzbuzz(n):
    # actual fizzbuzz
    return arbitrary_divisor_responses([
        (3, "fizz"), (5, "buzz")])(n)

def fb_start_end(start, end):
    # fizzbuzz within a range
    return "\n".join([
        fizzbuzz(n) for n in range(start, end+1)])

if __name__ == "__main__":
    print(fb_start_end(1, 100))
view (26 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)