Are you ready to tackle the fizzbuzz test in Java? – J021

by Jun 16, 2015

DeegeU Java Course

The “Are you ready to tackle the fizzbuzz test in Java?” video is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.

Try at home

  1. Write fizzbuzz without referencing code
  2. Write fizzbuzz for 5 and 7
  3. Try creating errors in your program and compiling it

Transcript – Are you ready to tackle the fizzbuzz test in Java?

So we’ve covered for loops. We’ve seen the print statement. We should be able to tackle one of the most bizarre beginner interview questions out there. Print the numbers from 1 to 100, and fizzbuzz.

It sounds crazy, but I’ve seen on many coding sites that there are people out there who can’t do this. If you’ve been following along in the lessons so far, you have enough knowledge already to tackle the FizzBuzz question. And we haven’t touched object oriented programming yet! By the end of this video, you should be confident enough to answer this question.

Our goal for this lesson is to understand how to count to 100, variations on the question, and understand what is asked in implementing fizzbuzz Java test.

Question number 1. Write a program that prints the numbers from 1 to 100. Assuming you’ve already added the application class and main method, let’s add the code that is doing something.

Inside the main method, we need to loop from 1 to 100. We saw this already with the all the loop statements we’ve looked at in previous lessons. Use a for-loop.

We’ll loop where i equals 1, continue while i is less than or equal to 100. The less than
or equal is important. If someone asks you this, pay attention to if 100 is included or not. If you’re not sure, ask them what they want. We’ll include 100. And increment i by 1, so i++.

Inside our for-loop block, we print i with System.out.println.

That’s it. We are printing from 1 to 100. We saw this in the last lesson.

What if we need to go from 100 to 1? Reverse the for-loop. So in this case, we loop starting at 100. Continue while i is greater than or equal to 1. Again the equals is important. And increment by i–.

That’s it. We are printing from 100 to 1.

One variation I saw was count from 100 to 1, starting with the for-loop where i starts at 1. In other words your for-loop had to start with for (int i=0; Where do you go from here? Well we need to loop 100 times, so we might as well enter the code from earlier. So here we’re printing from 1 to 100. The question asked to print from 100 to 1.

It’s a trick. A dumb trick, but according to one interviewer, many didn’t get it. Maybe it’s nerves in an interview? Just print 100 – i.

That’s it. We are printing from 100 to 1.

How about counting to 100, but you can’t use a for loop? That’s easy. Just create a variable i and set it to 0. Now using a while loop, we can set the condition while i is less than 100, print i. Easy.

The final looping interview question is the famous fizz-buzz question. For a while people were asking this, until it got around and people starting preparing for it. The question is print the numbers from 1 to 100. However, if the number is a multiple of 3 print fizz.
If it’s a multiple of 5 print buzz. If it’s a multiple of both, print fizz buzz. This is based on a popular preschool game designed to teach children multiples of numbers.

So we already are looping from 1 to 100. Lets use that. Now we just need to print the right values. The trick here is knowing about modulus arithmetic. If you remember in the operations lesson, we talked about the modulus operator. It returns the remainder when you do integer division. For something to be a multiple, the remainder must equal 0.

The FizzBuzz Java Test - Free Java Course Online - DeegeU

The FizzBuzz Java Test

Armed with that knowledge, we just need to test i to see if its a multiple before printing it. So we use an if then else statement.

If i mod 3 is 0 and i mod 5 is 0 print fizzbuzz. You could also do mod 15. If you don’t remember why, you can review the numbers lessons. Specifically modulo congruent.

Else if i mod 3 is 0 print fizz.

Else if i mod 5 is 0 print buzz.

Else print the number.

The other thing to note is to test if it’s a factor of both first. Otherwise you might get a fizz when you needed a fizzbuzz.

And that’s all there is to that question. So if anyone asks you this question, you should get it right. Next lesson we’ll start looking at Java classes, and object oriented programming!

Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Get the code

The source code for “Are you ready to tackle the fizzbuzz test in Java?” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don’t miss another video!

New videos come out every week. Make sure you subscribe!



DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This