Logo

Programming-Idioms

  • Python

Idiom #272 Play FizzBuzz

Fizz buzz is a children's counting game, and a trivial programming task used to affirm that a programmer knows the basics of a language: loops, conditions and I/O.

The typical fizz buzz game is to count from 1 to 100, saying each number in turn. When the number is divisible by 3, instead say "Fizz". When the number is divisible by 5, instead say "Buzz". When the number is divisible by both 3 and 5, say "FizzBuzz"

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)
s, a, b = '', 'Fizz', 'Buzz'
for x in range(1, 101):
    if not x % 3: s = a
    if not x % 5: s = s + b
    print(s or x)
    s = ''
for i in range(1, 100+1):
    out = ""
    if i % 3 == 0:
        out += "Fizz"
    if i % 5 == 0:
        out += "Buzz"
    print(out or i)
for i in range(100, 1):
    if i % 5 == 0 and not i % 3 == 0:
        print(i, "Buzz");
    if i % 3 == 0 and not i % 5 == 0:
        print(i, "Fizz");
    if i % 3 == 0 and i % 5 == 0:
        print(i, "FizzBuzz");
n=1
while(n<=100):
    out=""
    if(n%3==0):
        out=out+"Fizz"
    if(n%5==0):
        out=out+"Buzz"
    if(out==""):
        out=out+str(n)
    print(out)
    n=n+1
for(int i = 1; i <= 100; i++)
{
    if((i % 15) == 0) std::cout << "FizzBuzz" << std::endl;
    else if((i % 5) == 0) std::cout << "Buzz" << std::endl;
    else if((i % 3) == 0) std::cout << "Fizz" << std::endl;
    else std::cout << i << std::endl;
}

New implementation...
< >
fizzbuzz