Hey, guys, what's up?
So today we are going to solve one more interesting problem.
So the problem statement is given in it was it will give us and and we have to find some of even numbers.
From one to and so you it will give us the value of N.
And we have to find the sum of even numbers starting from one billion, for example, and equals five.
So even numbers are two plus for some output should be six for enclose five mild butchered B6.
Similarly to the value of N is eight.
So output should be two plus four plus six plus eight, which is 20.
So output should be 20.
OK.
And similarly, let's say the value of an is three.
So now it will be to.
OK, so if you remember, we have already solved this problem in flowcharts, in pseudocode.
But today we will be writing the actual code.
Okay, so how we can solve this problem.
So first of all, what I will do, I will take no scene and as in.
OK.
So for finding the sum what I need, I need a loop.
I need a variable.
Let's call it some.
And we will add the values to the some of the variable some.
So I need a loop and this loop will start from two.
So I need a loop.
And this loop will start from two three.
I is less than ordered close to N.
And each time what I will do I will add the value of.
I do my variable some.
And here I equals a plus two.
So why I am incrementing by two, because we have to consider even numbers.
And the first even number is two.
So the first two numbers two.
And that's why I initialised I equals two.
And since we have to go see this, we have to consider only even numbers.
So I will increase the value of Ibai to.
OK, so after running this look, my variable sum will be containing my answer and I will print some
here, see out some.
So this is all that we have to do.
So first, let us write code.
So let us name this file as.
Some of even Nazi BP.
Now, what I have to do, first of all, I will take a variable in and I will take and as input from
the user.
So I need a variable sum which will hold my answer.
So I need a loop which will start from two do, because two is the first even number.
And this loop will run while I is less than ordered close to end.
And each time this loop is learning what I have to do, I have to add the current even number to my
variable sum.
So some equals some plus I and I have to go to next even number.
So next year when number can be obtained by adding two to my current given number.
So this is all that we have to do.
And then I will print some which is holding my answer.
OK.
So this is all that we have to do.
What the output will be wrong.
Why?
Let's see.
So let's say the value of any is three.
So our purchase, we do so Oddworld is coming out to be a very strange number.
OK.
So let's see.
Let's bring it on our program and let's see what went wrong.
So the value of and is three.
OK.
So the first event number is two.
So I equals two ISBN initialised way to him because two is the first event number.
And this loop will run till I is less than articles two.
And so if I start watching, this condition is two less than articles two three.
So is two less than articles today.
So the condition is true.
I will go inside the loop.
So I will add the current even number for my variable sum.
So some equals current value of some plus current even number, which is to.
But what happened here is what is the value of getting some?
So I don't know if you remember when we had it, when we arrived in sum.
So what will happen this some variable will be given for bites of memory and the name of these four
bites out of some.
OK, so we are not right.
So we are not writing anything here.
So by default, there is a combination of zero one zero zero one zero and so on.
So this really is called garbage value.
Garbage value.
So is my son.
Variable is containing garbage value.
And it should have contain zero.
So let me show you.
So here I am.
Printing led to the value of some.
So I am printing some here.
And it's commented out.
Led to the value of and these two.
So this is the valley of some Myozyme is containing any random number.
And this number is called garbage because it is of no use to us.
So what we should do?
We have created the variable sum we have to initialize, initialize it with zero.
OK, so initially my sum is zero.
And now our code is hundred percent.
Correct.
So let's see.
So let's turn our program low to the value of amnesty.
So our budget we do, which is hundred percent correct.
Four and equals five, six, four, eight, 20.
So five and eight.
OK, so friend equals five.
My output is six.
And for any equals, eight wild four days.
Don't be so recorders.
Hundred percent correct.
But let's see how it works.
So our goal is very simple.
And I'm taking a variable sum, which is initialized to zero because initially the sum will be zero.
And my first event number is two.
And this loop is will be running while I is less than or equal to N and each time I will add the current
sum.
The current even number.
So I is containing current even number.
I is containing currently one number and I am adding that current event number to my variable sum.
OK.
Now I have to go to next event number.
So for going to next event number, I will add two to my current division number.
And finally, when this loop will terminate, my answer will be stored in my variable sum.
So this is the whole code.
Now, let us test our program for any quiz.
Let's say five.
So initially, the value of PHI is two.
And initially my sum is zero.
So check the condition.
So is to list then or close to five.
Yes, the condition is true and I do the same.
So sum, it was all value of sum, which is zero plus two.
So some equations do now increase the value of Highway two.
So a new value of EI is for this next to number.
And my sum is two.
So check the condition.
So is it for less than articles two?
Five is food listin or close to five.
Yes, the condition is true.
So sum it goes oil away.
Leave some which is two plus I, which is for the sum equals six.
Now inglese the value of EI noticeable next to a number.
So I equals six and my sum is containing six year.
Now, check the condition again.
So is six less than or close to five?
No.
The condition is false, since the condition is false.
I will.
This loop will get terminated and I will reach at this point and I am printing some.
So I am printing six, which is correct answer because two plus four equals six for N equals five.
So this is how our code is working.
So the thing that we have done is called Dryden Diren means you're on your code manually on pen and
paper to see how things are working to get a better understanding of your code.
So what changes can we do here is so we can also write like this?
Instead of writing some, it simply say we can write some blows equals a. they both are the same.
Similarly, instead of writing a class, I pledge to I can write a blessed equals two.
I'll get to the meaning will remain the same.
This is just another way in which we can write.
Now to the value of these five, some outproduce six.
OK, so what I told you, you can write like some it, some place I or some classic close eye.
They both are same.
Similarly I was a plus two or I plus equals two.
They both seem similarly.
I was I you multiply by two or I might reply equals two.
They were without him.
Similarly I equals I two and I they will die same.
Similarly and let's say I equals I multiply by seven so I multiply equals seven.
So do the same.
OK, this is just our way of writing.
Okay.
It doesn't make any difference.
It is just a way of writing.
OK, so the code was pretty simple and easy.
So I hope you understood this problem.
So if you have any doubt, feel free to ask.
OK.
So that is all for this video.
Thank you.