Logo

Programming-Idioms

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

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"

(1..100).each do |i|
  d3 = i % 3 == 0
  d5 = i % 5 == 0

  if d3 && d5
    puts "FizzBuzz"
  elsif d3
    puts "Fizz"
  elsif d5
    puts "Buzz"
  else
    puts "#{i}"
  end
end
#include <iostream>
#include <string>
for (int n=1; n<=100; n++) {
        string out="";
        if (n%3==0) {
            out=out+"Fizz";
        }
        if (n%5==0) {
            out=out+"Buzz";
        }
        if (out=="") {
            out=out+std::to_string(n);
        }
        cout << out << "\n";
    }

New implementation...
< >
fizzbuzz