All language subtitles for 4. Print the sum of all numbers from 1 to num - Solution

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian Download
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,720 --> 00:00:08,690 All right, so let's start diving into coding and in this exercise, what we have to do is to write 2 00:00:08,700 --> 00:00:11,060 a program that gets some numb, OK? 3 00:00:11,400 --> 00:00:20,730 And this program, what it should do is simply to calculate, calculate the sum of all the values from 4 00:00:20,730 --> 00:00:28,350 one to three and so on, up to num and print the final sum. 5 00:00:28,380 --> 00:00:31,440 OK, so preened creates the sum. 6 00:00:32,190 --> 00:00:34,620 So this exercise is not very difficult. 7 00:00:34,650 --> 00:00:39,080 Again, it's very similar to the exercises that we've already solved. 8 00:00:39,450 --> 00:00:47,940 So in print and turnham and read the num value, that's the first step that what we should do and not 9 00:00:47,940 --> 00:00:49,230 writing it once again. 10 00:00:49,240 --> 00:00:52,620 So we are just using this code snippet. 11 00:00:52,680 --> 00:00:59,950 OK, and now we are going to do is simply to use some for a loop for example. 12 00:00:59,970 --> 00:01:07,200 OK, so let's use it for a loop and create additional variable called AI and this variable will be used 13 00:01:07,200 --> 00:01:08,320 for our loop. 14 00:01:08,340 --> 00:01:14,370 So for I equals to zero as long as I use less than or equal to num. 15 00:01:14,550 --> 00:01:22,320 OK, basically let's start it from one end on every duration we are going to increment I by one and 16 00:01:22,320 --> 00:01:23,250 that's how you do it. 17 00:01:23,560 --> 00:01:29,400 Then let's use the following approach so we should, we know that we should sum all the values. 18 00:01:29,430 --> 00:01:35,240 So for that let's create a new variable and call it some let's initialism to be equal to zero. 19 00:01:35,700 --> 00:01:43,230 So on every duration we are going to take the previous value of sum and add to this value the value 20 00:01:43,230 --> 00:01:43,790 of PI. 21 00:01:43,800 --> 00:01:51,350 So some the new sound value equals to the previous some value plus the value of AI. 22 00:01:51,690 --> 00:01:52,250 All right. 23 00:01:53,100 --> 00:01:54,540 And that's how you calculate it. 24 00:01:54,550 --> 00:01:56,510 So some equals the sum plus I. 25 00:01:57,000 --> 00:02:00,000 So if we have like I don't know, Nomikos to five. 26 00:02:00,270 --> 00:02:06,600 So you get here with equals to one and we know that some first of all equals to zero, then some will 27 00:02:06,600 --> 00:02:09,090 be equal to one because it's zero plus one. 28 00:02:09,280 --> 00:02:12,660 Then I will be equal to two because I will be incremented. 29 00:02:13,110 --> 00:02:20,070 So it will be one plus two, it will be three and then one three plus three, it will be six and so 30 00:02:20,070 --> 00:02:23,940 on up until you reach in this case to what it is. 31 00:02:24,060 --> 00:02:25,260 Fifty fifty. 32 00:02:25,290 --> 00:02:25,580 Yeah. 33 00:02:26,100 --> 00:02:27,290 So it will be 15. 34 00:02:28,110 --> 00:02:34,770 So that's basically a simple solution on how you can iterate over all the numbers, all the natural 35 00:02:34,770 --> 00:02:39,720 numbers from one to NUM and calculate them and add them to this sum. 36 00:02:39,990 --> 00:02:47,370 And finally, what you have to do is simply to print out a solution so that the sum of all the numbers 37 00:02:47,730 --> 00:02:54,140 and numbers from one to two percentage need is the Geany. 38 00:02:54,390 --> 00:02:54,740 Right. 39 00:02:55,080 --> 00:03:00,030 So now instead of these percentages, we will use NOM, right. 40 00:03:00,870 --> 00:03:06,360 Because the sum of all the numbers from one up to ngom is in here. 41 00:03:06,360 --> 00:03:09,920 We will use instead of this placeholder, we will use some. 42 00:03:10,740 --> 00:03:11,880 So very simple. 43 00:03:12,380 --> 00:03:17,550 OK, so let's build and run it and see that it works correctly. 44 00:03:17,560 --> 00:03:20,430 So let's go enter Nomsa five. 45 00:03:20,760 --> 00:03:26,160 The sum of all the numbers from one up to five is fifteen, which is correct. 46 00:03:26,430 --> 00:03:31,420 And we also see I've seen an example of using seven, so let's go like seven. 47 00:03:31,800 --> 00:03:37,910 So the sum of all the numbers from one up to seven is twenty eight and that's freaking awesome. 48 00:03:37,920 --> 00:03:38,980 That's amazing guys. 49 00:03:39,000 --> 00:03:41,760 It works exactly as we expected it to. 50 00:03:42,270 --> 00:03:46,740 So if you still have any questions, feel free to ask them right now. 51 00:03:47,070 --> 00:03:50,010 And one thing that I want to show you is how it can be done. 52 00:03:50,010 --> 00:03:56,580 Also using the while loop, because some of you may have solved this exercise using while loop. 53 00:03:56,580 --> 00:04:03,150 So let's go with a while loop and basically with the while loop it maybe even easier because I don't 54 00:04:03,150 --> 00:04:04,020 need this I. 55 00:04:04,320 --> 00:04:06,240 OK, and I can just go like this. 56 00:04:06,240 --> 00:04:07,590 So let's try this approach. 57 00:04:07,600 --> 00:04:19,800 So while num is greater or equal two to one, ok then in this case let's use some equals to some plus 58 00:04:19,980 --> 00:04:24,720 num case or will it simply take five. 59 00:04:24,750 --> 00:04:28,530 OK, and we need just to decrement it now minus Munnings. 60 00:04:28,920 --> 00:04:31,490 So if num equals to five the condition is true. 61 00:04:31,500 --> 00:04:38,760 So we take some equals, do some plus five and then num will become for some plus four and so on. 62 00:04:38,770 --> 00:04:44,160 So we simply start from the end to make this sum from the end to one. 63 00:04:44,160 --> 00:04:47,250 So num num minus one plus, so on, so forth. 64 00:04:47,400 --> 00:04:49,020 Plus three, plus two plus one. 65 00:04:49,230 --> 00:04:54,570 As long as num is greater or equal to one, the only shurat are coming in. 66 00:04:54,570 --> 00:05:00,060 This solution is that you kind of lose the value of NUM if you need it for further. 67 00:05:00,330 --> 00:05:07,560 Analysis, so just like in this case where we print this message, the sum is probably going to be calculated 68 00:05:07,770 --> 00:05:08,630 as it should. 69 00:05:08,670 --> 00:05:10,380 OK, but what do we have here? 70 00:05:10,740 --> 00:05:18,210 As long as nothing is greater or equal to one, then let's check. 71 00:05:18,210 --> 00:05:19,820 What is the problem? 72 00:05:19,830 --> 00:05:20,950 What is the problem? 73 00:05:20,970 --> 00:05:23,700 OK, so we're expecting before. 74 00:05:23,710 --> 00:05:25,500 Oh, sorry about that. 75 00:05:26,610 --> 00:05:30,420 OK, so that's how it is a problem. 76 00:05:30,420 --> 00:05:32,460 So let's build and run it and see what happens. 77 00:05:32,490 --> 00:05:37,970 So there are number five, so the sum of all the numbers from one up to zero is 15. 78 00:05:38,310 --> 00:05:40,710 So the sum was calculated correctly. 79 00:05:41,070 --> 00:05:46,930 But these numbers, OK, which was replaced in these placeholder is zero. 80 00:05:46,950 --> 00:05:48,990 Why is it zero and zero? 81 00:05:48,990 --> 00:05:51,600 Because I told you guys that that's the problem. 82 00:05:51,600 --> 00:05:56,670 If we are using this approach, like if we want to rely on these numbers, let's run. 83 00:05:57,750 --> 00:05:59,280 These numbers will be decrements. 84 00:05:59,280 --> 00:06:04,280 So one way to solve it is simply to use the additional temp variable that will help us. 85 00:06:04,290 --> 00:06:08,610 So in temp and here we will use like temp equals to. 86 00:06:08,610 --> 00:06:17,250 No, just making a copy of NARM in here instead of now we will use temp once again so that if we need 87 00:06:17,250 --> 00:06:22,880 to print the value of Naameh afterwords, we will not lose it because we used here some temp variable. 88 00:06:23,160 --> 00:06:30,330 So in this case I think that both of the options for for a loop and while both of them require you to 89 00:06:30,330 --> 00:06:36,810 use additional variable, if you want to rely on these numbers after the loop is over. 90 00:06:36,820 --> 00:06:41,010 So let's build and run it finally to make sure that everything works as expected. 91 00:06:41,020 --> 00:06:42,340 So let's go with seven. 92 00:06:42,840 --> 00:06:47,280 So the sum of all the numbers from one up to seven is twenty eight, which is correct. 93 00:06:48,270 --> 00:06:49,710 So yeah. 94 00:06:49,710 --> 00:06:55,350 So now you've seen how it can be done using while loops and using for loops. 95 00:06:55,350 --> 00:07:02,070 And that's just I'm just curious about what will be the sum from one up to ten which it will be fifty 96 00:07:02,070 --> 00:07:02,460 five. 97 00:07:02,760 --> 00:07:03,310 Awesome. 98 00:07:03,720 --> 00:07:11,160 So you know how it can be done using the wire loop and using the for a loop which is freaking awesome. 99 00:07:11,970 --> 00:07:15,190 So with that being said, thank you for watching. 100 00:07:15,210 --> 00:07:16,520 Thank you for practicing. 101 00:07:16,890 --> 00:07:23,540 And you are expected to receive more difficult exercises in the next videos. 102 00:07:23,910 --> 00:07:26,460 So until then, have a great time. 103 00:07:26,670 --> 00:07:27,630 Take a break. 104 00:07:27,810 --> 00:07:29,130 Drink something nice. 105 00:07:29,130 --> 00:07:30,810 Eat something, some snack, maybe. 106 00:07:30,810 --> 00:07:31,410 Maybe not. 107 00:07:31,650 --> 00:07:32,930 I don't know, whatever you like. 108 00:07:33,210 --> 00:07:34,290 So my name is a lot. 109 00:07:34,320 --> 00:07:35,760 Guys, this is Alphatech. 110 00:07:35,760 --> 00:07:36,750 Thank you so much. 111 00:07:36,750 --> 00:07:39,240 And I will see you in the next videos. 10293

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