4. Calculating the remainder of a division | Get paid course for Free | Comment Down your Language |

Hi guys and welcome back. In this video we’re going to look at how to get the remainder of a division. And this can be a really useful thing when we are trying to check whether a number is odd or even, and that itself also has a number of uses. Let’s start by doing something like division with remainder. Remainder like that, and we’re going to do 12 divided by five. So if print this out, then you’ll see that the output is just two. Even though 12 divided by five should give you two, remainder two. That’s because five goes into 12 two times, and you have two left over, so two is the remainder. You can get the remainder by just doing 12 % five, and this is essentially the counterpart to integer division because it gives you the remainder of the division. So if we run this like that, you’ll see that you get two in the first instance of 12 divided by five, and then you also get two which is the remainder in 12 divided by five there. Now if we change this to 13, you’ll see that the numbers change. The first one will still be two, but the second one will now be three, because the remainder of 13 divided by five is three. So why is getting the remainder so popular? Why is it useful? Why does Python have it’s own operator just for that? Look at this numbers, and you’ll spot a pattern probably. What is the remainder of 10 divided by two? Or 14 divided by two, or six divided by two, or 340 divided by two, the remainder in all of these is zero. Because they are even numbers divided by two. Similarly if you have something like 11 divided by two, or 27 divided by two, or three divided by two, the remainder in all of these cases is always one. So one of the reasons why this operator exists and why it’s so popular in programming is because it very easily allows you to determine whether a number is odd or even. Indeed for every even number, the remainder when divided by two is zero. And for every odd number, the remainder when divided by two is one. So let’s look at some code. X is 37, the remainder we can do x % two, and then we can print out whether it is odd or even by printing this out. So if this value is one, that means that it is odd, and if it is zero, that means that it is even. So at this point I’m going to delete this code here just before running. And now let’s run this. Usually that we get one back, which tell us this number here is odd. Think about this, have you ever seen a table online where each row has a different colour? So for example the first row is grey, the second one is white, third one’s grey, the fourth one’s white, so you’ve got like this sort of pattern? This is a perfect example of where the remainder, or being able to calculate whether a number is odd or even, comes into play. They are probably using this to only target the even rows and colour them in a grey background. Throughout the course we may also encounter other instances where this can be useful, but that’s it for this video. Thank you for joining me, and I’ll see you in the next one.

Leave a Reply

Your email address will not be published. Required fields are marked *