WEBVTT

0
00:01.280 --> 00:01.760
All right.

1
00:01.760 --> 00:05.570
In this lesson, we're going to learn about a new operator.

2
00:05.570 --> 00:12.740
So similar to the other mathematical operators and the conditional operators, an operator is just a

3
00:12.740 --> 00:17.300
symbol in programming that has a specific function.

4
00:17.660 --> 00:19.940
So what is the modulo operator?

5
00:19.940 --> 00:25.730
Well, it looks a bit like a percentage sign, but it's not just the percentage sign.

6
00:26.420 --> 00:29.000
Instead it goes in between two numbers.

7
00:29.000 --> 00:37.090
So hence a binary operator, and it works out what is the remainder after the division.

8
00:37.090 --> 00:41.080
So 10 / 5 = 2.

9
00:41.080 --> 00:48.700
And this division is clean because five goes into ten two times and there's no remainder.

10
00:48.700 --> 00:58.200
So therefore ten modulo five, if we were to print this out would give us the result of zero (10 % 5 = 0), no remainder

11
00:58.200 --> 00:59.460
in this division.

12
00:59.640 --> 01:01.920
So here's a challenge for you.

13
01:01.950 --> 01:04.920
What is 10 % 3?

14
01:04.920 --> 01:09.030
If 10 / 3 is 3.333333...

15
01:09.120 --> 01:15.120
recurring, because ten does not divide cleanly into three, what do you think would be the result of

16
01:15.120 --> 01:17.400
10 % 3 on a computer?

17
01:18.230 --> 01:19.910
Pause and have a think about it.

18
01:22.160 --> 01:25.070
Okay, so let's go through the solution together.

19
01:25.100 --> 01:33.530
If ten divided by three is equal to three with some decimal places, that means ten does not divide

20
01:33.530 --> 01:34.910
cleanly into three.

21
01:35.120 --> 01:40.640
Ten divided by three is equal to three with one as the remainder.

22
01:40.640 --> 01:47.620
So that means 10 % 3  is going to equal to 1, because all that the modulo does is it performs

23
01:47.620 --> 01:53.500
a division and then gives us how many is remaining after that division.

24
01:53.500 --> 01:58.510
So armed with that knowledge, you are now ready to tackle another coding challenge.

25
01:58.510 --> 02:05.920
And in this coding challenge, I want you to write some code to check whether the number inserted in

26
02:05.920 --> 02:14.520
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

27
02:14.520 --> 02:15.450
input area.

28
02:15.450 --> 02:23.250
You'll need to convert it into an integer number, and then you'll need to use an if statement to check

29
02:23.250 --> 02:27.960
and see if you were to modulo that number,

30
02:28.050 --> 02:31.790
is it going to have a remainder?

31
02:31.790 --> 02:39.140
And remember that even numbers, when you use the modulo,

32
02:39.140 --> 02:43.160
so for example let's take an even number, 12.

33
02:43.160 --> 02:51.260
If you use the modulo and you divide that number by two, it's always going to be equal to zero, because

34
02:51.260 --> 02:55.960
any even number divided by two should divide cleanly.

35
02:55.960 --> 03:01.420
So if it divides cleanly with no remainder, then it is an even number.

36
03:01.420 --> 03:02.410
So have a

37
03:02.410 --> 03:07.090
think about this and see if you can complete this challenge.

38
03:07.090 --> 03:14.890
And then later on, in a minute, we will pause the video and then I will come back and explain the solution

39
03:14.890 --> 03:15.520
together.

40
03:15.970 --> 03:17.470
So give it a go now.

41
03:22.140 --> 03:27.000
Okay, so the first thing we need to do is to get hold of an input.

42
03:27.030 --> 03:29.220
Now, it doesn't matter what you ask.

43
03:29.340 --> 03:34.530
You could say, "What is the number you want to check?"

44
03:35.070 --> 03:40.680
And this is going to come out onto here.

45
03:40.680 --> 03:48.480
And then once the user types a number, that number needs to be stored and converted into an integer.

46
03:48.480 --> 03:51.090
So let's do the type conversion first.

47
03:51.090 --> 03:58.680
And then let's create a variable called, number_to_check, and set it equal to this right-hand side.

48
03:58.680 --> 04:01.500
So let's just confirm that that worked.

49
04:01.500 --> 04:06.450
Let's go ahead and hit Run, type in a number, and it gets printed.

50
04:06.450 --> 04:09.260
So that's all working as expected.

51
04:09.980 --> 04:13.730
Now the next thing we need to do is to actually check the number.

52
04:14.270 --> 04:23.990
So if we print the number_to_check % 2, to check if it's even, then we can try this out with

53
04:23.990 --> 04:25.130
some different inputs.

54
04:25.130 --> 04:26.990
So let's try 12.

55
04:26.990 --> 04:29.360
Modulo is zero.

56
04:29.360 --> 04:32.090
Now what if we try an odd number.

57
04:32.090 --> 04:34.360
Let's try a three.

58
04:34.630 --> 04:40.450
Then we get modulo gives us an output of one.

59
04:40.450 --> 04:49.510
So 3 % 2, has a remainder of one, which means it's not even because it does not cleanly divide

60
04:49.510 --> 04:50.440
into two.

61
04:50.470 --> 04:53.710
Let's try another odd number 19.

62
04:53.710 --> 05:02.790
So you can see from this experiment or your knowledge of maths, whatever we take and we modulo

63
05:02.790 --> 05:11.940
it by two if it's equal to zero, if this result is equal to zero, then that means that this number

64
05:11.940 --> 05:13.440
is an even number.

65
05:13.440 --> 05:17.940
So instead of printing this we can use an if statement.

66
05:18.660 --> 05:22.800
If number_to_check % 2 == 0:

67
05:22.800 --> 05:24.060
Now,

68
05:24.060 --> 05:26.550
on an indented

69
05:26.700 --> 05:29.490
line of code, we can say print.

70
05:29.490 --> 05:37.590
Well, this is obviously going to be an even number, and otherwise, so else, then we can print that

71
05:37.590 --> 05:39.810
this is an odd number.

72
05:40.020 --> 05:47.200
So let's just add one space here so that when I run this my cursor just has one space beyond the question

73
05:47.200 --> 05:47.710
mark.

74
05:47.740 --> 05:49.510
Let's check some numbers.

75
05:49.510 --> 05:53.170
45 is odd, which is true.

76
05:53.650 --> 05:56.710
34 should be even.

77
05:56.830 --> 06:04.390
So this is reasonably simple, but it's also quite hard because it uses two things that you've learnt,

78
06:04.390 --> 06:07.360
and it combines them together.

79
06:07.360 --> 06:12.840
So hopefully you managed to get this to work and you manage to write the code yourself.

80
06:12.840 --> 06:16.920
If not, head back to your code and fix it so that it works,

81
06:16.920 --> 06:20.730
after you have seen the solution. There's no shame in seeing the solution,

82
06:20.730 --> 06:22.710
it's all a part of the learning process.

83
06:22.710 --> 06:24.870
Don't worry if you didn't get it first time.

84
06:24.870 --> 06:29.250
There's a lot of things that I didn't get first time as well, but the important thing is you write

85
06:29.250 --> 06:35.220
down notes and you understand what is explained in the lessons so that you're ready to tackle the next

86
06:35.220 --> 06:35.910
challenge.