# The infamous FizzBuzz test

*Last updated on October 29, 2018 by Michael Lossagk , 12 min •*

## 1. Task

Here is the textual description for the FizzBuzz algorithm:

- Write a short program that prints every number from 1 to 100 in a new line (or column).
- For each multiple of 3, write “Fizz” instead of the number.
- For each multiple of 5, write “Buzz” instead of the number.
- For numbers that are a multiple of 3 and 5, write “FizzBuzz” instead of the number.

The instructions are quite manageable and the task does not sound that difficult, but what do we do best here? This question will be clarified step by step in the next chapters.

## 2. Output numbers from 1 to 100 in a new row (or column)

For the ordered output of numbers we use a simple counting loop. In Java we use the `for-loop`

.

```
for (int i = 1; i <= 100; i++) {...}
```

Since we should start at 1, we write `int i = 1`

as the starting value and, analogously, we stop the loop at 100 with `i <= 100`

. We want to check every single number in the interval between $1 ... 100$, so we increase the loop variable in each run by one `i++`

.

## 2. For each multiple of 3, write “Fizz” instead of the number

The numbers resulting from the multiplication of a number $a$ with $1, 2, 3, ..., n$ are called *multiples of a*.
Example: The multiples of $3$ are: $3, 6, 9, 12, 15, ...$. In other words, all numbers that can be divided by 3 without remainder are multiples of this number, so $3/3 = 1, 6/3 = 2, 9/3 = 3, ...$ This property can be checked in Java using the modulo operator $%$. The statement `3 % 3`

yields the result `0`

, as well as eg `6 % 3`

,`9 % 3`

and `12 % 3`

. Thus, we can simply check for `i % 3 == 0`

if we want to know if a number is divisible by 3 without a remainder.

```
if (i % 3 == 0) {
System.out.print("i is divisible by 3 without remainder");
}
```

We can now combine this with the loop and get:

```
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0) {
System.out.print("Fizz ");
} else {
System.out.print(i + " ");
}
}
```

The output is

```
1 2 Fizz 4 5 Fizz 7 8 Fizz 10 11 Fizz 13 14 Fizz 16 17 ...
```

Thus, the condition is fulfilled that we output all numbers from 1 to 100, but for all numbers that are divisible by 3, write *Fizz* instead of the number. Similarly, the next section shows how we satisfy the condition for each multiple of 5.

## 3. For each multiple of 5, write “Buzz” instead of the number

The procedure for this condition is almost identical to the one in the previous section. The multiples of $5$ are: $5, 10, 15, 20, 25, ...$. In other words, all numbers that can be divided by 5 without remainder are multiples of this number, so $5/5 = 1, 10/5 = 2, 15/5 = 3, ...$, so `5 % 5 == 0, 10 % 5 == 0, 15 % 5 == 0`

, etc. The code snippet looks like this:

```
if (i % 5 == 0) {
System.out.print("i is divisible by 5 without remainder");
}
```

We can now combine this with the loop and get:

```
for (int i = 1; i <= 100; i++) {
if (i % 5 == 0) {
System.out.print("Buzz ");
} else {
System.out.print(i + " ");
}
}
```

The output is

```
1 2 3 4 Buzz 6 7 8 9 Buzz 11 12 13 14 Buzz 16 17 18 19 Buzz 21 22 23 24 Buzz ...
```

Thus, the condition is fulfilled that we output all numbers from 1 to 100, but for all numbers that are divisible by 5, write *Buzz* instead of the number. Now 3 out of 4 conditions are met. Let us now dedicate ourselves to the last condition.

## 4. For numbers that are a multiple of 3 and 5, write “FizzBuzz” instead of the number

For this condition we have to calculate the least common multiple (lcm) of 3 and 5. Due to the fact that the two numbers are primes, the lcm is found quickly. We just have to multiply the two numbers and get $3 x 5 = 15$. Thus, 15 is the multiple of 3 and 5, which means that we can test for the following condition:

```
if (i % 15 == 0) {
System.out.print("i is divisible by 15 without remainder");
}
```

We can now combine this with the loop and get:

```
for (int i = 1; i <= 100; i++) {
if (i % 5 == 0) {
System.out.print("FizzBuzz ");
} else {
System.out.print(i + " ");
}
}
```

The output is

```
1 2 3 4 5 6 7 8 9 10 11 12 13 14 FizzBuzz 16 17 18 19 20 21 22 23 24 25 26 27 28 29 FizzBuzz 31 32 33 34 35 36 37 38 39 40 41 42 43 44 FizzBuzz ...
```

Thus, the last condition is fulfilled. We output all numbers from 1 to 100 and write for all numbers divisible by 3 and 5 (= 15) *FizzBuzz* instead of the number. Now we have to combine the conditions, which will be shown in the next chapter.

## 5. FizzBuzz

We have now met the individual condition, but they still need to be combined to get the desired result. Here we must note that we have the condition for divisibility by 3 and 5 as the first condition. This is important because otherwise a wrong result is generated. Here is an example

```
if (i % 3 == 0) {
System.out.print("Fizz ");
} else if (i % 5 == 0) {
System.out.print("Buzz ");
} else if (i % (3 * 5) == 0) {
System.out.print("FizzBuzz ");
} else {
System.out.print(i + " ");
}
```

If, as in the example above, we test first for divisibility by 3, then 5, and then 3 and 5 together, we would never output *FizzBuzz*, since any number divisible by 3 and 5 is also divisible by 3 is. Therefore, only this condition is always met. The wrong result would look like this:

```
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 Fizz ...
```

As you can see, *FizzBuzz* is never printed (written together, don´t be fooled by ‘Fizz ‘space’ Buzz`). For this reason, it is important to first test for divisibility by 3 and 5 together. Thus we convert the code as follows:

```
if (i % (3 * 5) == 0) {
System.out.print("FizzBuzz ");
} else if (i % 3 == 0) {
System.out.print("Fizz ");
} else if (i % 5 == 0) {
System.out.print("Buzz ");
} else {
System.out.print(i + " ");
}
```

The output is now

```
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 ...
```

As you can see, 3 is replaced by *Fizz*, which replaces 5 with *Buzz*, 6 replaces *Fizz*, 9 replaces *Fizz*, 10 replaces *Buzz*, 12 replaces *Fizz*, and finally replaces 15 with *FizzBuzz*, and so on.
So we have fulfilled all conditions and solved the task. Here is the complete code:

```
public void doFizzBuzz() {
for (int i = 1; i <= 100; i++) {
// print 'FizzBuzz' in place of the number for numbers divisible by 3 and 5
if (i % (3 * 5) == 0) {
System.out.print("FizzBuzz ");
// print 'Fizz' in place of the number for numbers divisible by 3
} else if (i % 3 == 0) {
System.out.print("Fizz ");
// print 'Buzz' in place of the number for numbers divisible by 5
} else if (i % 5 == 0) {
System.out.print("Buzz ");
} else {
System.out.print(i + " ");
}
}
}
```

Test it yourself online (if it does not work with Chrome, then allow third party cookies or use a different browser):

## 6. Conclusion

In this tutorial, I presented the infamous FizzBuzz algorithm. This algorithm is so interesting because, despite its simplicity, it is often asked in job interviews. Frighteningly, many applicants have problems with it which can quickly lead to rejection in such a conversation and thus one has no chance of the desired employment. The appropriate code examples can be found **in my GitLab repository**. If you want to learn more about Java and programming I can recommend you **this book**.