1 00:00:00,150 --> 00:00:02,009 All right guys, it's time for yet 2 00:00:02,009 --> 00:00:05,730 another code challenge to test and see if you've learned what was covered in 3 00:00:05,730 --> 00:00:08,910 previous lessons. So head over to the assignment 4 00:00:08,910 --> 00:00:11,490 that is a day 3.1 odd 5 00:00:11,490 --> 00:00:16,490 or eve. Have a read of the instructions and try to see if you can create a 6 00:00:17,010 --> 00:00:22,010 program that checks whether if a number that you input in the console is odd 7 00:00:23,070 --> 00:00:26,040 or even. Now the thing to remember here though 8 00:00:26,100 --> 00:00:29,640 is that we're introducing something called the modulo 9 00:00:30,330 --> 00:00:34,650 and you can create the symbol by simply typing the percentage sign. Now, 10 00:00:34,680 --> 00:00:38,820 what the modulo does is it will divide a number 11 00:00:39,030 --> 00:00:43,830 say seven by another number 12 00:00:43,830 --> 00:00:47,700 let's say two, and then it will give you the remainder of that division. 13 00:00:48,030 --> 00:00:53,030 So seven divided by two, seven split into portions of two is two plus two plus 14 00:00:57,150 --> 00:00:59,700 two plus one, right? 15 00:01:00,090 --> 00:01:05,090 So seven divided by two can be divided three times with a remainder of one. 16 00:01:06,060 --> 00:01:07,830 And that's what the modulo gives you. 17 00:01:07,950 --> 00:01:12,120 So you can also type code straight into the console like I'm going to do now; 18 00:01:12,390 --> 00:01:14,850 7 % 2. It gives me 1. 19 00:01:16,020 --> 00:01:20,130 Now it's the same thing if you try to divide by other numbers, 20 00:01:20,130 --> 00:01:23,130 let's say 7 % 3. Well, 21 00:01:23,130 --> 00:01:28,110 that is three plus three plus one, right? 22 00:01:28,320 --> 00:01:32,790 So seven modular three will also give me one. 23 00:01:34,170 --> 00:01:38,430 So this is a way for us to divide a number by another number and get the 24 00:01:38,430 --> 00:01:39,270 remainder. 25 00:01:40,620 --> 00:01:45,620 And you're going to be using this modular to solve this code challenge. 26 00:01:46,500 --> 00:01:47,910 Have a read of the instructions, 27 00:01:47,940 --> 00:01:52,940 have a look at the examples and see what it is that we're expecting to be 28 00:01:53,310 --> 00:01:55,290 printed in the console here. 29 00:01:56,070 --> 00:02:01,070 And then go ahead and write your code and see if your output matches what it is 30 00:02:01,620 --> 00:02:05,040 that we're expecting. So 56 should be even, 31 00:02:05,370 --> 00:02:09,660 so you should be printing "This is an even number" based on that input. 32 00:02:10,500 --> 00:02:12,990 Pause the video now and give this code challenge a go. 33 00:02:17,300 --> 00:02:17,660 All right. 34 00:02:17,660 --> 00:02:22,340 So I mentioned that the modulo gives us the remainder, right? 35 00:02:22,820 --> 00:02:27,820 So what do we know about even numbers? Well even numbers they're always divisible by 36 00:02:29,330 --> 00:02:34,070 two. If we pick any even number, say 54, 37 00:02:34,430 --> 00:02:38,240 and then we do modulo two, so 54 divided by 2, 38 00:02:38,570 --> 00:02:40,670 it should always give us zero 39 00:02:40,850 --> 00:02:45,710 as long as that first number is even so 68 % 2 40 00:02:45,800 --> 00:02:46,880 again it zero. 41 00:02:47,150 --> 00:02:51,590 But then when we come across an odd number, say 53 % 2, 42 00:02:52,010 --> 00:02:53,990 then this is now going to give us 1. 43 00:02:54,710 --> 00:02:59,710 We can use our code to check for when the result is zero, 44 00:03:00,250 --> 00:03:04,900 in which case it's a even number. And when the result is one, 45 00:03:04,960 --> 00:03:09,310 in which case it's an odd number. If you have gotten stuck until this point, 46 00:03:09,580 --> 00:03:13,750 and this is a really great time to pause the video again and go back and give it 47 00:03:13,750 --> 00:03:17,020 another go. And then we'll go through the solution together. 48 00:03:18,910 --> 00:03:23,910 We can simply write an if statement to check if the number that was inputted is 49 00:03:26,470 --> 00:03:31,120 divisible by two with zero remainder. 50 00:03:31,930 --> 00:03:36,310 Well, in this case, then we can pretty much be sure that this is an even number, 51 00:03:36,760 --> 00:03:41,200 because as we've shown, all of the even numbers when they divide by two, 52 00:03:41,290 --> 00:03:45,820 there's always no remainder. So in this case, let's add a colon. 53 00:03:46,360 --> 00:03:51,360 We can print pretty confidently that this is a even number. 54 00:03:53,590 --> 00:03:53,920 Now, 55 00:03:53,920 --> 00:03:58,920 what about the cases where the number when divided by two is not equal to zero? 56 00:04:00,010 --> 00:04:03,610 Well, then it's probably going to be an odd number. So in this case 57 00:04:03,610 --> 00:04:05,950 we can use an else statement to catch that. 58 00:04:06,580 --> 00:04:10,210 And when we have an odd number, we'll simply print 59 00:04:10,210 --> 00:04:12,730 this is an odd number. 60 00:04:13,450 --> 00:04:18,450 So that's it. Just four the lines of code using what we've learned about if, else, 61 00:04:19,570 --> 00:04:22,780 and also the comparison operator. Now, 62 00:04:22,810 --> 00:04:25,750 if you got stuck on this and you needed to watch the solution, 63 00:04:26,110 --> 00:04:30,340 then I recommend really going back to your code and playing around with it so 64 00:04:30,340 --> 00:04:34,990 that you can get the code to work. And then try and see if you can test if a 65 00:04:34,990 --> 00:04:39,990 particular number is cleanly divisible by three by four by five, 66 00:04:40,630 --> 00:04:44,620 and then see if the final program actually works in the way that you expect it 67 00:04:44,620 --> 00:04:46,690 to. But once you're ready, 68 00:04:46,840 --> 00:04:50,620 head over to the next lesson where we're going to talk about even more complex 69 00:04:50,620 --> 00:04:54,820 ways of using if and else statements. So for all of that 70 00:04:55,030 --> 00:04:56,140 and more, I'll see you there.