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 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 even
touched object oriented programming yet! By the end of this video,
you should be confident enough to answer this question. Our goals 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. Write a program that prints the numbers from 1 to 100.
Assuming you've already added the application class and main method,
let's jump in and add the code that's 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
had 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 here is really important. If someone asks you
this, pay attention to whether 100
is included or not. If you're not sure, ask them what they want.
We're going to include 100 and implement i by 1.
So i++
What if we need to go from 100 to 1?
Reverse the for-loop. So in this case,
we're 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
started at 0.
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 here we're printing
from one 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. Make sure you’re not including 100, or you’ll print 0. Unless the interviewer wants zero.
Again ask, don’t guess. That's it!
We are printing from 100 to 1.
How about counting to 100, but you can't use a for loop?
Well… can’t use this.
Just create a variable i and set it to 1.
Now using a while loop, we can set the condition while i is less than or equal
to 100.
print i. Easy. The final the big question is the famous fizzbuzz 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".
f it's a multiple of 5 print "buzz".
If it's a multiple of 5 print buzz. If it’s a multiple of both, print "fizzbuzz".
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.
So in this case, 12 is a multiple of 3,
so the modulus is 0.
5 is not a factor of 12, so it’s modulus is nonzero.
Armed with that knowledge, we just need to test i
and see if its a multiple before printing it. So we use an if-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 congruence modulo
in the negative numbers lesson. Else if i mod 3 is 0 print fizz.
Else if i mod 5 is 0 print buzz.
Otherwise print the number. The only thing to note
The other thing to note is to test if it's a factor of both first. Otherwise you might get a fizz when you
really needed a fizzbuzz. And that's all there is to this 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!
Hey thanks for watching the video!
There is a quick quiz for this on DeegeU.com if you'd like to gauge how much you
learned.
If you like the videos you are seeing, please let me know by liking the video
and hitting the subscribe button for
the DeegeU channel on YouTube. I'd really appreciate that. If you have concerns or
questions, please leave them in the comments below.
There's a poll on the front page of DeegeU.com
so you can let me know the topic covered
next. Thanks for watching, and see you in the next video!