[ create a new paste ] login | about

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

C:
pasted 5 hours ago:
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

int main(){
int i;
  for(i=1;i<=100;i++){
    if(i%3==0)  printf("Fizz");
    if(i%5==0)  printf("Buzz");
    if(i%3!=0&&i%5!=0) printf("%d",i);
    printf("\n");
}
return 0;
}
view (12 lines, 100 lines of output)
pasted on May 16:
/*FizzBuzzの判定を行うプログラム*/

//デバッグが終わったらコメントアウトを外す
#define NDEBUG

#include <stdio.h>
#include <assert.h>

#define SAIDAI 100		//判定する値の最大値(最小は1からとする)
#define FIZZ 3			//Fizzと判定する倍数
#define BUZZ 5			//Buzzと判定する倍数
#define FIZZBUZZ (FIZZ*BUZZ)	//FizzBuzzと判定する倍数



/*プロトタイプ宣言*/
int func();

int main(void){

/*変数宣言*/
	int max=SAIDAI;
	int f=FIZZ;
	int b=BUZZ;
	int fb=FIZZBUZZ;
	int i;

/*FizzBuzz判定を行うfunc関数を、指定回数ループさせる*/

	for(i=1;i<max+1;i++){
		func(i,f,b,fb);
	}

/*assertでテストを行う*/
#if !defined(NDEBUG)
	printf("ここから下はassertの出力です\n");
#endif
	assert(func(1,3,5,15)==3);
	assert(func(3,3,5,15)==1);
	assert(func(5,3,5,15)==2);
	assert(func(15,3,5,15)==0);

	return 0;

}

/*FizzBuzz判定を行うfunc関数

引数  int i	判定する数字
	int f	Fizzと判定する倍数
	int b	Buzzと判定する倍数
	int fb	FizzBuzzと判定する倍数

戻り値 int hantei:判定結果を代入する変数
		hantei=0 FizzBuzz
		hantei=1 Fizz
		hantei=2 Buzz
		hantei=3 それ以外(出力は数字そのものを表示)
*/
int func(int i,int f,int b,int fb){
	
	int hantei=-1;
	
	if(i%fb==0){
		printf("FizzBuzz\n");
		hantei=0;

	}else if(i%f==0){
		printf("Fizz\n");
		hantei=1;

	}else if(i%b==0){
		printf("Buzz\n");
		hantei=2;

	}else{
		printf("%d\n",i);
		hantei=3;
	}
	return hantei;
}
view (81 lines, 100 lines of output)


C++:
pasted on May 24:
#include <iostream>
#include <stdlib.h>

using namespace std;

// with c++11 no need for a helper function, but codepad compiler is old.
string to_string(int i) {
  char buf[10];
  sprintf(buf, "%d", i);
  return buf;
}

#define F(x) << (!((x)%15)?"fizzbuzz":!((x)%3)?"fizz":!((x)%5)?"buzz":to_string(x)) << endl
#define E(a,b,c,d) F(a*20+b*4+c*2+d+1)
#define D(a,b,c) E(a,b,c,0)E(a,b,c,1)
#define C(a,b) D(a,b,0)D(a,b,1)
#define B(a) C(a,0)C(a,1)C(a,2)C(a,3)C(a,4)
#define FB B(0)B(1)B(2)B(3)B(4)

int main() {
  cout FB;
}
view (22 lines, 100 lines of output)
pasted on May 24:
#include <iostream>
#include <stdlib.h>

using namespace std;

// with c++11 no need for a helper function, but codepad compiler is old.
string to_string(int i) {
  char buf[10];
  sprintf(buf, "%d", i);
  return buf;
}

#define F(x) << (!((x)%15)?"fizzbuzz":!((x)%3)?"fizz":!((x)%5)?"buzz":to_string(x)) << endl
#define E(a,b,c,d) F(a*20+b*4+c*2+d+1)
#define D(a,b,c) E(a,b,c,0)E(a,b,c,1)
#define C(a,b) D(a,b,0)D(a,b,1)
#define B(a) C(a,0)C(a,1)C(a,2)C(a,3)C(a,4)
#define FB B(0)B(1)B(2)B(3)B(4)

int main() {
  cout FB;
}
view (22 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 May 25:
1
2
3
4
5
6
7
8
9
10
11
<?php
for ($i=1; $i <= 100; ++$i) { 
	switch ($i) {
		case (($i%3)===0): echo 'Fizz';if(($i%5)!==0)break;
		case (($i%5)===0): echo 'Buzz';break;
		default:
			echo $i;
			break;
	}
        echo "\n";
}
view (11 lines, 100 lines of output)
pasted on May 19:
1
2
3
4
5
6
7
8
9
10
11
<?php
for ($i=1; $i <= 100; ++$i) { 
	switch ($i) {
		case (($i%3)===0): echo 'Fizz';if(($i%5)!==0)break;
		case (($i%5)===0): echo 'Buzz';break;
		default:
			echo $i;
			break;
	}
	echo PHP_EOL;
}
view (11 lines, 100 lines of output)


Perl:
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)
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 May 24:
1
2
3
4
5
6
7
8
9
for i in range(1, 101):
  if i % 15 == 0:
    print "fizzbuzz"
  elif i % 5 == 0:
    print "buzz"
  elif i % 3 == 0:
    print "fizz"
  else:
    print i
view (9 lines, 100 lines of output)
pasted on May 24:
1
2
3
4
5
6
7
8
9
for i in range(1, 101):
  if float(i) % 15 == 0:
    print "fizzbuzz"
  elif float(i) % 5 == 0:
    print "buzz"
  elif float(i) % 3 == 0:
    print "fizz"
  else:
    print i
view (9 lines, 100 lines of output)


Ruby:
alikhosroshahi81 - pasted on May 11:
1
2
3
4
5
6
7
8
9
10
11
(1..100).each do |numbers|
	if numbers % 3 == 0 && numbers % 5 == 0
		puts 'fizzbuzz'
	elsif numbers % 3 == 0
		puts 'fizz'
	elsif numbers % 5 == 0
		puts 'buzz'
	else
		puts numbers
	end
end		
view (11 lines, 100 lines of output)
pasted on May 10:
1
2
3
4
5
6
7
8
9
10
11
(1..100).each do |numbers|
	if numbers % 3 == 0 && numbers % 5 == 0
		puts 'fizzbuzz'
	elsif numbers % 3 == 0
		puts 'fizz'
	elsif numbers % 5 == 0
		puts 'buzz'
	else
		puts numbers
	end
end		
view (11 lines, 100 lines of output)