Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
All right guys, it's time for yet
2
another code challenge to test and see if you've learned what was covered in
3
previous lessons. So head over to the assignment
4
that is a day 3.1 odd
5
or eve. Have a read of the instructions and try to see if you can create a
6
program that checks whether if a number that you input in the console is odd
7
or even. Now the thing to remember here though
8
is that we're introducing something called the modulo
9
and you can create the symbol by simply typing the percentage sign. Now,
10
what the modulo does is it will divide a number
11
say seven by another number
12
let's say two, and then it will give you the remainder of that division.
13
So seven divided by two, seven split into portions of two is two plus two plus
14
two plus one, right?
15
So seven divided by two can be divided three times with a remainder of one.
16
And that's what the modulo gives you.
17
So you can also type code straight into the console like I'm going to do now;
18
7 % 2. It gives me 1.
19
Now it's the same thing if you try to divide by other numbers,
20
let's say 7 % 3. Well,
21
that is three plus three plus one, right?
22
So seven modular three will also give me one.
23
So this is a way for us to divide a number by another number and get the
24
remainder.
25
And you're going to be using this modular to solve this code challenge.
26
Have a read of the instructions,
27
have a look at the examples and see what it is that we're expecting to be
28
printed in the console here.
29
And then go ahead and write your code and see if your output matches what it is
30
that we're expecting. So 56 should be even,
31
so you should be printing "This is an even number" based on that input.
32
Pause the video now and give this code challenge a go.
33
All right.
34
So I mentioned that the modulo gives us the remainder, right?
35
So what do we know about even numbers? Well even numbers they're always divisible by
36
two. If we pick any even number, say 54,
37
and then we do modulo two, so 54 divided by 2,
38
it should always give us zero
39
as long as that first number is even so 68 % 2
40
again it zero.
41
But then when we come across an odd number, say 53 % 2,
42
then this is now going to give us 1.
43
We can use our code to check for when the result is zero,
44
in which case it's a even number. And when the result is one,
45
in which case it's an odd number. If you have gotten stuck until this point,
46
and this is a really great time to pause the video again and go back and give it
47
another go. And then we'll go through the solution together.
48
We can simply write an if statement to check if the number that was inputted is
49
divisible by two with zero remainder.
50
Well, in this case, then we can pretty much be sure that this is an even number,
51
because as we've shown, all of the even numbers when they divide by two,
52
there's always no remainder. So in this case, let's add a colon.
53
We can print pretty confidently that this is a even number.
54
Now,
55
what about the cases where the number when divided by two is not equal to zero?
56
Well, then it's probably going to be an odd number. So in this case
57
we can use an else statement to catch that.
58
And when we have an odd number, we'll simply print
59
this is an odd number.
60
So that's it. Just four the lines of code using what we've learned about if, else,
61
and also the comparison operator. Now,
62
if you got stuck on this and you needed to watch the solution,
63
then I recommend really going back to your code and playing around with it so
64
that you can get the code to work. And then try and see if you can test if a
65
particular number is cleanly divisible by three by four by five,
66
and then see if the final program actually works in the way that you expect it
67
to. But once you're ready,
68
head over to the next lesson where we're going to talk about even more complex
69
ways of using if and else statements. So for all of that
70
and more, I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.