All language subtitles for 8. Print n even natural numbers - Solution

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian Download
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,650 --> 00:00:08,990 OK, so welcome back to the Solution video, and hopefully you tried to solve this exercise by coming 2 00:00:08,990 --> 00:00:16,550 up with at least one or two solutions for this problem, one solution that uses conditions and the other 3 00:00:16,550 --> 00:00:18,620 solution that does not use condition. 4 00:00:19,130 --> 00:00:26,450 So let's start with, first of all, reminding of what this exercise is all about and then continuing 5 00:00:26,720 --> 00:00:33,740 with the first option, the first solution, discussing what is what are we going to use there, how 6 00:00:33,740 --> 00:00:40,430 it's going to be implemented, and then move on to the second solution and also talk about probably 7 00:00:40,820 --> 00:00:48,850 some bugs that you may get while using the second solution and how it may be also avoided. 8 00:00:49,370 --> 00:00:54,230 So get yourself something nice to drink and let's start in. 9 00:00:54,230 --> 00:00:58,450 The first thing that we are going to do is just to make some reminder. 10 00:00:58,790 --> 00:01:10,220 So what we want to do is just to print no even numbers starting starting from the value of two. 11 00:01:10,360 --> 00:01:18,140 OK, so we've seen one example that if we have nonliving equals to five, then in this case what we 12 00:01:18,140 --> 00:01:21,440 will do is just print all the values from two. 13 00:01:22,550 --> 00:01:24,650 Basically no time now. 14 00:01:24,650 --> 00:01:26,740 Even values starting from two. 15 00:01:26,750 --> 00:01:31,960 OK, two, four, six, eight, 10, a total of five or num values. 16 00:01:32,690 --> 00:01:33,800 So awesome. 17 00:01:34,640 --> 00:01:41,060 And with that being said, let's show them basically the preferrable solution. 18 00:01:41,060 --> 00:01:41,890 Start with it. 19 00:01:42,260 --> 00:01:45,260 And the solution includes using some loops. 20 00:01:45,530 --> 00:01:45,940 Right. 21 00:01:45,990 --> 00:01:48,650 Of course the first one and the second one probably will. 22 00:01:48,650 --> 00:01:49,250 Both of them. 23 00:01:49,940 --> 00:01:52,550 Both of them will include the usage of Loopt. 24 00:01:52,850 --> 00:01:56,370 And in the first one we will use it for a loop. 25 00:01:57,050 --> 00:02:00,170 So for a for a loop, let's create additional variable. 26 00:02:00,170 --> 00:02:07,790 I and also, of course, let's create not just made the usual process of Antonoff and then reading the 27 00:02:07,790 --> 00:02:14,800 value from the user and storing it inside the variable number cited variable noun. 28 00:02:15,700 --> 00:02:19,460 Now that we are ready, we can move on to using the for loop. 29 00:02:20,230 --> 00:02:23,220 So basically, what should be the for loop? 30 00:02:23,230 --> 00:02:30,220 What should we what would you need all be able to for I equals to one as long as I use less than or 31 00:02:30,220 --> 00:02:31,680 equal to none. 32 00:02:32,260 --> 00:02:35,510 So that's what we are going to do. 33 00:02:35,530 --> 00:02:38,620 And the question is, if this solution is OK, what do you think? 34 00:02:38,620 --> 00:02:42,310 So printf percentage being just printing the value of it. 35 00:02:42,730 --> 00:02:44,640 So take a second and think about it. 36 00:02:44,920 --> 00:02:46,590 Do you think the solution is OK? 37 00:02:46,630 --> 00:02:53,170 What will it print, for example, if we get I don't know here, if we get Nomikos to five, what will 38 00:02:53,170 --> 00:02:54,490 be printed on the screen? 39 00:02:55,300 --> 00:02:58,780 Basically the solution is very simple, right? 40 00:02:58,780 --> 00:03:02,200 It will print all the values from one up to none, and that's it. 41 00:03:02,590 --> 00:03:04,880 But that's not something that we want, right? 42 00:03:04,900 --> 00:03:07,870 Because if we will build and run it, then we will see. 43 00:03:08,770 --> 00:03:09,550 Yeah. 44 00:03:09,550 --> 00:03:17,770 And we will see basically guys sorry for this transition and just needed to fix something in the code 45 00:03:17,770 --> 00:03:18,120 blocks. 46 00:03:18,400 --> 00:03:20,770 So once again, let's make a quick reminder. 47 00:03:20,770 --> 00:03:28,000 We basically created these for a loop and we want to see how it works if we build and run it. 48 00:03:28,000 --> 00:03:29,410 So build and run. 49 00:03:29,410 --> 00:03:36,550 And we will be able to see in our console application that if we insert the value of five, then in 50 00:03:36,550 --> 00:03:40,570 this case all the numbers from one up to five will be printed. 51 00:03:41,300 --> 00:03:48,340 But that's not something that we want because we want to print all the even numbers starting from two. 52 00:03:49,030 --> 00:03:53,530 And the main question that you should ask yourself, basically, how should you do it? 53 00:03:53,530 --> 00:04:01,060 You can't simply print from starting from one and how you will need to print two, four or six and so 54 00:04:01,060 --> 00:04:03,090 on, not Naameh times. 55 00:04:03,430 --> 00:04:07,840 So there are basically even here using these for a loop. 56 00:04:08,110 --> 00:04:09,880 There are a couple of options. 57 00:04:10,210 --> 00:04:16,990 And the first option is simply to realize that if you have a sequence of numbers starting from one, 58 00:04:17,380 --> 00:04:24,640 two, three, four, let's say we had five, then in this case it will simply simply print the value 59 00:04:24,940 --> 00:04:27,190 of I multiplied by two. 60 00:04:27,520 --> 00:04:31,240 We'll print the following sequence two, four, six, eight and 10. 61 00:04:32,510 --> 00:04:32,970 Right. 62 00:04:33,430 --> 00:04:40,750 So we will have like to understand that if we start from equals to one and we multiplied by two, we 63 00:04:40,750 --> 00:04:44,950 simply start these four, all of these printing operations from two. 64 00:04:44,980 --> 00:04:46,000 And we would do it. 65 00:04:46,630 --> 00:04:48,420 How many times sometimes. 66 00:04:48,850 --> 00:04:52,110 OK, so that's one option to solve this exercise. 67 00:04:52,120 --> 00:04:55,690 So let's let's build and run it and let's go. 68 00:04:55,690 --> 00:04:56,290 Let's go, let's go. 69 00:04:56,290 --> 00:04:56,650 Come on. 70 00:04:56,770 --> 00:04:59,780 OK, so now equals two five. 71 00:04:59,980 --> 00:05:01,150 So there you go. 72 00:05:01,150 --> 00:05:03,670 Two, four, six, eight and 10. 73 00:05:04,540 --> 00:05:06,610 And that's also a good question. 74 00:05:06,640 --> 00:05:07,630 A good question. 75 00:05:07,630 --> 00:05:10,570 I'm saying a good solution to this exercise. 76 00:05:10,960 --> 00:05:15,670 And another option that we can use is simply going like this. 77 00:05:15,670 --> 00:05:18,040 So let's say that's the first option. 78 00:05:18,130 --> 00:05:18,620 OK. 79 00:05:18,640 --> 00:05:21,570 Option number one, OK. 80 00:05:21,600 --> 00:05:23,550 And we will also put it in comment. 81 00:05:24,670 --> 00:05:27,430 That's basically how you create a multi line comment. 82 00:05:27,970 --> 00:05:30,370 And now let's go with option number two. 83 00:05:30,370 --> 00:05:33,220 So option number two goes like this. 84 00:05:33,370 --> 00:05:40,540 We simply didn't have any clue about these combination that we can multiply every value of I by two 85 00:05:40,540 --> 00:05:47,100 and getting like the sequence that we want exactly like it was requested in the exercise. 86 00:05:47,380 --> 00:05:49,420 So the second condition can go like this. 87 00:05:49,450 --> 00:05:56,380 So for I equals to two, the second option, because we know that we should start printing from two 88 00:05:56,680 --> 00:06:03,490 and we should do it as long as I use less than or equal to, to what that we you have to think about 89 00:06:03,490 --> 00:06:05,220 it as long as want. 90 00:06:05,770 --> 00:06:07,930 So that's one question. 91 00:06:07,930 --> 00:06:09,200 What should we put here. 92 00:06:09,580 --> 00:06:15,790 One option is maybe let's try using here as long as I is less than or equal to num multiplied by two. 93 00:06:16,090 --> 00:06:17,270 Is this option OK? 94 00:06:17,920 --> 00:06:18,820 Probably not. 95 00:06:19,720 --> 00:06:26,580 So we can see that using the for a loop is also optional, but it will be kind of problematic right 96 00:06:26,580 --> 00:06:26,740 now. 97 00:06:27,370 --> 00:06:30,910 So one other option that we would use is a while loop. 98 00:06:30,910 --> 00:06:34,650 So as long as none is greater than zero. 99 00:06:35,170 --> 00:06:39,730 So in this case we will initialize EI of the beginning to be equal to two. 100 00:06:40,270 --> 00:06:47,500 And then what we will do is just printing the value of I OK, the value of EI. 101 00:06:48,040 --> 00:06:55,030 So first of all we will print two and then we need to increment or basically multiply the value of I 102 00:06:55,390 --> 00:06:55,990 by two. 103 00:06:56,170 --> 00:06:56,520 Right. 104 00:06:56,530 --> 00:06:58,240 So it will be something like that. 105 00:06:58,510 --> 00:07:05,590 I multiplied by two because if we multiplied two will get different numbers, but I plus equals to two. 106 00:07:05,980 --> 00:07:12,550 So basically taking the previous value of EI and multiplying and adding to it the value of two. 107 00:07:12,550 --> 00:07:14,950 So I equals to EI plus two. 108 00:07:15,660 --> 00:07:23,070 OK, and also what we have to do, because what will be the problem, the solution, if we run these 109 00:07:23,070 --> 00:07:27,980 wire loop and we build and run it, then in this case, what will happen is very simple. 110 00:07:28,110 --> 00:07:29,330 Let me show you guys. 111 00:07:29,850 --> 00:07:37,980 So if you do something like this, you enter five and then you press enter, then you will see all the 112 00:07:37,980 --> 00:07:42,430 numbers and your program is probably going to get stuck. 113 00:07:42,930 --> 00:07:45,310 So how to avoid it? 114 00:07:45,570 --> 00:07:50,800 So we know that these wireless currently is an infinite loop and we want to avoid it. 115 00:07:51,180 --> 00:07:57,750 So in this case, what we will need to do is simply to use numbers equals to the previous number, minus 116 00:07:57,780 --> 00:08:02,820 one, because we want to do this operation just some times. 117 00:08:02,970 --> 00:08:05,400 So that's basically how you can do it. 118 00:08:05,410 --> 00:08:08,330 So build and run it. 119 00:08:08,340 --> 00:08:11,710 And let's use the value of seven for this example. 120 00:08:11,730 --> 00:08:15,360 So two, four, six, eight, 10, 12 and 14. 121 00:08:15,750 --> 00:08:17,530 And there you go. 122 00:08:18,390 --> 00:08:24,720 So to summarize the two options that I came across and showed you guys, of course, there are also 123 00:08:24,720 --> 00:08:29,400 additional options that we may use using you for a loop and also the wire loop. 124 00:08:29,640 --> 00:08:38,370 But that's basically the two most common options, the two most common solutions that my students tend 125 00:08:38,370 --> 00:08:38,910 to use. 126 00:08:39,270 --> 00:08:44,860 So that's why I brought you these solutions to this exercise. 127 00:08:45,870 --> 00:08:48,240 So, yeah, take your time, guys. 128 00:08:48,240 --> 00:08:52,480 Try to solve it on your own if you did not do it successfully. 129 00:08:52,500 --> 00:08:59,450 And I wish you a great day and good luck with this exercise and with our following exercises. 130 00:08:59,490 --> 00:09:02,760 My name is Vlad and I will see you in the next video. 12111

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.