Sunday, March 04, 2012

Everybody Loves FizzBuzz

One of my career objectives is implementing business requirements in a way that makes
  • customer / employer happy
  • developers happy, and
  • infrastructure happy
at the same time. I'd like to take FizzBuzz puzzle as an example to illustrate how attention to detail helps me achieve this goal in Java.
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

public class FizzBuzz {

    public static void main(String[] args) {
        boolean fizzOrBuzz;

        for (int i = 1; i <= 100; i++) {
            fizzOrBuzz = false;

            if (i % 3 == 0) {
                fizzOrBuzz = true;
                System.out.print("Fizz");
            }

            if (i % 5 == 0) {
                fizzOrBuzz = true;
                System.out.print("Buzz");
            }

            if (!fizzOrBuzz) {
                System.out.print(i);
            }

            System.out.println();
        }
    }
}

Update (06/12/2014): https://github.com/codingsince1985/FizzBuzz