Logo

Programming-Idioms

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

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"

#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