All right.
In this lesson, we're going to learn about a new operator.
So similar to the other mathematical operators and the conditional operators, an operator is just a
symbol in programming that has a specific function.
So what is the modulo operator?
Well, it looks a bit like a percentage sign, but it's not just the percentage sign.
Instead it goes in between two numbers.
So hence a binary operator, and it works out what is the remainder after the division.
So 10 / 5 = 2.
And this division is clean because five goes into ten two times and there's no remainder.
So therefore ten modulo five, if we were to print this out would give us the result of zero (10 % 5 = 0), no remainder
in this division.
So here's a challenge for you.
What is 10 % 3?
If 10 / 3 is 3.333333...
recurring, because ten does not divide cleanly into three, what do you think would be the result of
10 % 3 on a computer?
Pause and have a think about it.
Okay, so let's go through the solution together.
If ten divided by three is equal to three with some decimal places, that means ten does not divide
cleanly into three.
Ten divided by three is equal to three with one as the remainder.
So that means 10 % 3 is going to equal to 1, because all that the modulo does is it performs
a division and then gives us how many is remaining after that division.
So armed with that knowledge, you are now ready to tackle another coding challenge.
And in this coding challenge, I want you to write some code to check whether the number inserted in
the input area so typed by a user is odd or even, so you'll need to get hold of what they type in the
input area.
You'll need to convert it into an integer number, and then you'll need to use an if statement to check
and see if you were to modulo that number,
is it going to have a remainder?
And remember that even numbers, when you use the modulo,
so for example let's take an even number, 12.
If you use the modulo and you divide that number by two, it's always going to be equal to zero, because
any even number divided by two should divide cleanly.
So if it divides cleanly with no remainder, then it is an even number.
So have a
think about this and see if you can complete this challenge.
And then later on, in a minute, we will pause the video and then I will come back and explain the solution
together.
So give it a go now.
Okay, so the first thing we need to do is to get hold of an input.
Now, it doesn't matter what you ask.
You could say, "What is the number you want to check?"
And this is going to come out onto here.
And then once the user types a number, that number needs to be stored and converted into an integer.
So let's do the type conversion first.
And then let's create a variable called, number_to_check, and set it equal to this right-hand side.
So let's just confirm that that worked.
Let's go ahead and hit Run, type in a number, and it gets printed.
So that's all working as expected.
Now the next thing we need to do is to actually check the number.
So if we print the number_to_check % 2, to check if it's even, then we can try this out with
some different inputs.
So let's try 12.
Modulo is zero.
Now what if we try an odd number.
Let's try a three.
Then we get modulo gives us an output of one.
So 3 % 2, has a remainder of one, which means it's not even because it does not cleanly divide
into two.
Let's try another odd number 19.
So you can see from this experiment or your knowledge of maths, whatever we take and we modulo
it by two if it's equal to zero, if this result is equal to zero, then that means that this number
is an even number.
So instead of printing this we can use an if statement.
If number_to_check % 2 == 0:
Now,
on an indented
line of code, we can say print.
Well, this is obviously going to be an even number, and otherwise, so else, then we can print that
this is an odd number.
So let's just add one space here so that when I run this my cursor just has one space beyond the question
mark.
Let's check some numbers.
45 is odd, which is true.
34 should be even.
So this is reasonably simple, but it's also quite hard because it uses two things that you've learnt,
and it combines them together.
So hopefully you managed to get this to work and you manage to write the code yourself.
If not, head back to your code and fix it so that it works,
after you have seen the solution. There's no shame in seeing the solution,
it's all a part of the learning process.
Don't worry if you didn't get it first time.
There's a lot of things that I didn't get first time as well, but the important thing is you write
down notes and you understand what is explained in the lessons so that you're ready to tackle the next
challenge.