FizzBuzz for (almost) Every Language

FizzBuzz. Chances are that you’ll need to know it at one time or another if you apply for any programming or development job. Here are a couple ways to do FizzBuzz in a variety of languages. There are alternate ways to do this in each language but these are a few examples of some code that you could use. I”ll update this post over time as I find new examples.

Python
The example below is a function which would allow you to specify your own minimum and maximum value for the code to evaluate. You could convert it to the standard fizzbuzz (which usually evaluates 1-100) by just including the code from the for statement down and replacing low and high with 1 and 101 respectively to evaluate 1-100.

def fizzbuzz(low, high):
  for i in xrange(low, high):
    if i%3 == 0 and i%5 == 0:
      print "FizzBuzz"
    elif i%3 == 0:
      print "Fizz"
    elif i%5 == 0:
      print "Buzz"
    else:
      print i

C# 2

// This is the "classic" solution to the FizzBuzz problem.
for (int i = 1; i <= 100; i++)
{
    if (i % 3 == 0 && i % 5 == 0)
        {
            Console.WriteLine("FizzBuzz");
        }
    else if (i % 3 == 0)
        {
            Console.WriteLine("Fizz");
        }
    else if (i % 5 == 0)
        {
            Console.WriteLine("Buzz");
        }
    else
        {
            Console.WriteLine(i.ToString());
    }
}
 
// This is a "contemporary" solution to the FizzBuzz problem using ONE LINE of LINQ!
Enumerable.Range(1, 100).ToList().ForEach(n => Console.WriteLine((n % 3 == 0) ? (n % 5 == 0) ? "FizzBuzz" : "Fizz" : (n % 5 == 0) ? "Buzz" : n.ToString()));

C 1

#include 

int main()
{
    int i;

    for (i = 0; i <lt;= 100; i++)
    {
        if (i % 15 == 0)
            printf("FizzBuzz");
        else if (i % 3 == 0)
            printf("Fizz");
        else if (i % 5 == 0)
            printf("Buzz");
        else
            printf("%d", i);

        printf("n";
    }
}

C++ 1

#include 
 
using namespace std;
 
int main()
{
    for (int i = 0; i <= 100; i++)
    {
        if (i % 15 == 0)
            cout << "FizzBuzz";
        else if (i % 3 == 0)
            cout << "Fizz";
        else if (i % 5 == 0)
            cout << "Buzz";
        else
            cout << i;
 
        cout << endl;
    }
}

Java 1

public class FizzBuzz
{
    public static void main(String [] args)
    {
        for (int i = 1; i <= 100; i++)
        {
            if (i % 15 == 0)
                System.out.print("FizzBuzz");
            else if (i % 3 == 0)
                System.out.print("Fizz");
            else if (i % 5 == 0)
                System.out.print("Buzz");
            else
                System.out.print(i);

            System.out.println();
        }
    }
}

Perl 1

#!/usr/bin/perl
 
for ($i = 1; $i <= 100; $i++)
{
    if ($i % 15 == 0) {
        print "FizzBuzz"
    }
    elsif ($i % 3 == 0) {
        print "Fizz";
    }
    elsif ($i % 5 == 0) {
        print "Buzz";
    }
    else {
        print $i;
    }
 
    print "n";
}

Javascript (execute in Firebug debugger) 1

for (var i=1; i <= 100; i++)
{
    if (i % 15 == 0)
        console.log("FizzBuzz");
    else if (i % 3 == 0)
        console.log("Fizz");
    else if (i % 5 == 0)
        console.log("Buzz");
    else
        console.log(i);
}

PHP 1

for ($i = 1; $i <= 100; $i++)
{
    if ($i % 15 == 0)
        print "FizzBuzz";
    elseif ($i % 3 == 0)
        print "Fizz";
    elseif ($i % 5 == 0)
        print "Buzz";
    else
        print $i;
 
    print "n";
}

Ruby 1

for i in 1..100
    if i % 15 == 0 then
        print "FizzBuzz"
    elsif i % 3 == 0 then
        print "Fizz"
    elsif i % 5 == 0 then
        print "Buzz"
    else
        print i
    end
 
    print "n"
end

Sources:
2. Original code was done by a guest on Pastebin – Link
FizzBuzz – Rosetta Code