# FizzBuzz

Question:
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." Apparently this question is used to weed out 99.5% of candidates. Be sure to know this one!

## The Fizz Buzz test

Apparently this is supposed to filter out 99.5% of programming job candidates. The reason why many can't solve such a simple problem is that it's hard to implement without code duplication.

## A probable solution

A good attempt at the problem would look something like this:

static void fizzBuzz(int num) {
for (int i = 0; i < num; i++) {
if (num % 3 == 0 && num % 5 == 0) {
System.out.println("fizzbuzz");
} else if (num % 3 == 0) {
System.out.println("fizz");
} else if (num % 5 == 0) {
System.out.println("buzz");
} else {
System.out.println(num);
}
}
}

## Without repetition

To avoid repetition, we can use StringBuilder.

public class FizzBuzz {
public static void main(String[] args) {
fizzBuzz(100);
}

static void fizzBuzz(int num) {

StringBuilder result = new StringBuilder();

for (int i = 0; i <= num; i++) {
result.append((i%3) == 0 ? "fizz" : "");
result.append((i%5) == 0 ? "buzz" : "");
String resultString = result.toString();
System.out.println(resultString.isEmpty() ? i : resultString);
}

}
}

## Sources

Why is Fizz-Buzz so hard?

Came up with a better solution or have a question? Comment below!