Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Elixir

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"

defmodule FizzBuzz do
  def run do
    for n <- 1..100 do
      case { rem(n, 3) == 0, rem(n, 5) == 0 } do
        { true, true } -> IO.puts("FizzBuzz")
        { true, false } -> IO.puts("Fizz")
        { false, true } -> IO.puts("Buzz")
        _ -> IO.puts(to_string(n))
      end
    end
  end
end

FizzBuzz.run
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