[ create a new paste ] login | about

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

C:
pasted on Nov 28:
int main()
{
	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 (26 lines, 100 lines of output)
pasted on Nov 25:
int main()
{
	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 (26 lines, 100 lines of output)


C++:
pasted on Nov 29:
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
stringstream ss;
for (int i = 1; i  < 101; i++)
{
ss.str(string ());
ss.clear ();
if (i % 3 == 0)
ss << "Fizz";
if (i%5 == 0)
ss << "Buzz";
if (ss.str() == "")
ss << i;
cout << ss.str() << endl;
}
return 0;
}
view (23 lines, 100 lines of output)
pasted on Nov 29:
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

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


Perl:
pasted on Aug 13:
1
2
3
4
5
6
(print ((plorg($_).plib($_)) or ($_))) and print "\n" for (1..100);



sub plorg{$_%3? '':'Fizz'}
sub plib{$_%5? '':'Buzz'}
view (6 lines, 100 lines of output)
pasted on Aug 2:
1
2
3
4
5
6
@FB1 = (1..100);
@FB2 = map{!($_%3 or $_%5)?'FizzBuzz': $_}@FB1;
@FB3 = map{(/\d/ and !($_%3))?'Fizz':$_}@FB2;
@FB4 = map{(/\d/ and !($_%5))?'Buzz':$_}@FB3;
@FB5 = map{$_."\n"}@FB4;
print @FB5;
view (6 lines, 100 lines of output)


Python:
Nayak2623 - pasted on Nov 25:
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 Nov 15:
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 Jul 12:
1
2
3
4
5
6
7
8
9
10
11
12
def fizz_buzz(numbers)
    
    numbers.each do |number|
       response = ""
       response += "Fizz" if number % 3 == 0
       response += "Buzz" if number % 5 == 0
       puts response.empty? ? number : response
    end
end

numbers = (1..100).to_a
fizz_buzz(numbers)
view (12 lines, 100 lines of output)