Hello, guys, and welcome back to the cause.
In the last class, I introduce a concept called Lube, and I told you that the loops are very, very
important and will be used throughout this course.
So today, really solving six problems based on loops, because I want to make sure that the fundamentals
are very, very strong.
So I have divided six problems into two parts.
Each part will consist of three problems.
Padrón problems are easy and part two problems that are a little tough for beginner.
OK.
So let's begin part one problem one solve the problem statement is given an.
We have to print all the numbers from where to win.
Bentall, the numbers from one to N, for example, let's say the value of An is five.
So now it butchered be one, two, three, four and five.
Let's see the value of an is six star Bush would be one, two, three, four, five and six.
Similarly to the value of an is to start Bush would be one and two.
And if the value of this one so large, which would be one.
So given n we have to print all the numbers from one to N.
So how can we solve this problem?
If you look carefully, we are printing five times to six times here, two times here and weren't done
here.
So we have to print any numbers starting from one to N.
So we need a loop that will then end times.
So the loop will look something like this.
I equals one.
Is a less than Articles two in.
And then I will do some work here and then I will increment the value of I buy one and then I will check
my condition again.
So this loop will run and times via this loop is running and times because I initialize the value if
I buy one and then I'm checking is a list and goes to.
And at each time this loop is running, I am incrementing the value if I buy one.
So this loop will run and times.
So the first part of our problem statement is over.
Now this loop will run and times.
So we have to bring five numbers here.
So this loop will in five times for an equals five.
As a middle of the value of any equals six.
This look will run six times.
Now, what will I do?
I will take a variable.
Well, real means value.
So initially my value is when.
So what will I do here?
I will print the value.
Brent.
Well, you and I will increment well by one.
So liquids well past one.
So initially, when will get printed?
Then two will get printed.
Then three will get printed and so on.
So this little Bazzani and times and each time every print val and the value of Val will be increased
by one.
So next time this blueprinted and the next time table printed and so on.
And the last end will be printed.
And then my loop will terminate.
My loop will break.
So let us draw the flowchart.
So I will start my flow chart.
Then why do they do for running the loop and times?
I will take a variable.
And I really flies it to one and similarly I will DEGA Well which is when I will bring value.
So.
As I left and it goes to in this condition is used to run the loop and Bems.
If this condition is true, what will I do?
I will bring developed.
And really grew in the well by van.
So when equals.
Well, plus when.
And now I will being guaranteed why when as I want to run the loop for end times.
So this loop will run and times because initially my eye equals one.
And each time this loop is learning, I am granting the value.
If I buy one.
So I equals I plus one.
So this loop will run and times.
And each time I am printing well.
And the value of well is increased by one.
If this condition is false, I will simply end my program.
OK.
Let's take an example and I will try to show you how this is working.
Let's say, and it goes five.
OK.
So N equals five.
I started my program, I equals one valid question.
OK.
Is I less than it was doing so is one less than I close to five.
Yes, the condition is true.
I will go here.
Brent.
Well, OK.
When will we printed?
It gives the value of well, by when.
So the new value of well is to OK, given the value, if I buy one to the new value, if I is to, then
I am checking.
The condition is to less than articles to five years.
Yes, the condition is true, Brent.
Well OK.
To leap into the new value of well is three and Grendell if I buy one.
So it's two less than it was when we had the condition is true.
So tabouli reprinted in the value.
Well by one.
So the new value of well is for.
And similarly the new interface for so is for less than I to.
Yes.
Print for.
And similarly print five and then my I will become six and six is not less than articles to five.
So this condition is false.
And I will come here and I will end my program.
And this is my output which is hundred percent correct.
If you have look carefully.
There is no need of when.
I am repeating myself.
If you look carefully, there is no need of variable van.
Basically, I and well are equal I and well are equal.
Look carefully.
Initially, both are initialized to one that I'm printing well and then guarantee the value of well
by one similar name granting the value if I buy one.
What can I do here instead of printing.
I will print I because I end when I see him, I will print.
And similarly, there is no need of this block.
I will simply if there are, if I buy one, just like I am increasing the value of men by one.
So let us know the updated flowchart.
So I will start my program.
They will only take a single variable line, which is initialized to one.
Then I will let the male Lupron for and times because we have to print and numbers.
So this conditional limit to initially.
I will print.
I hear instead of printing one, because I it will contain the same value.
Then I will simply increment the value if I buy one.
And then I will check the condition again.
And similarly here, I'll be having false and end.
Okay.
Let us run this code for an example.
Let's say let's say N equals three.
So I start my program.
I take a variable eight, which is initialized to one that I'm checking is one less than English.
Two, three.
So the condition is true.
So I will come here.
A..
When we liberated in Greece there ayliffe, I buy one.
So the new really if I is to OK then I will come here.
Is to less than to so the condition is true, I will come here.
Brent, I will have rendered increase the value if I buy one.
So the new value if I is three is still less than it was two.
Three.
Yes.
The condition is true.
You will three after printing.
Three after printing I, I will check.
Sorry.
I will increase the value if I buy one to the new value if I is four.
So it is for less than ogles.
Two, three.
So the condition is false and you will come here and you will end my program.
And the two terms went to three is printed.
So that is hundred percent correct.
So there is no need of variable.
Well if you are taking a variable.
Well today's notion that.
But there is actually no need.
Okay.
So it doesn't waste what the question was, the question was given.
And we have to print all the numbers from one to and basically we have to print and times starting from
when.
So here you seeds are my example, if anyone thinks we are printing six time, if any clues do we have
printing two thumbs and so on.
So initially, what was our approach?
Our approach was very simple.
I will take a variable eye and let this Lupron for N times because we have to print and numbers.
So the loop will run and times how this loop is running and times I am taking a very well I which is
initialized to one.
And each time this loop is running, I am grinding the way.
If I buy one.
So this loop will end and times simple and I will do some work here.
So the work is to print numbers.
So overprinting numbers.
What I did, I took a variable will and I will print.
Well, each time.
And after printing that one, I will increase the value of well by one.
Simple.
So Hayes the first version of our flowchart.
So what I did start my program.
Take two variable.
I end well initially.
Both are initialized to one.
Then my loop is ending.
End times.
Each time I'm printing when.
And incrementing the value of Val.
By one.
So this will be simple flow.
And then I told you there is no need of Vallandigham because they are equal.
So you start your program, you took a equals one, and this law will run and times.
Each time you are sprinting, I saw one, two, three, four, and of course, three will get printed.
OK, so let us all learn the problem.
Problem to.
So the verbal statement is.
Given an.
You have to check.
It is.
Brime.
Or not.
OK.
So first of all, what type prime numbers, prime numbers are.
Those numbers.
And that has only two factors.
One and itself.
For example, if the value of an is five star, output should be prime.
Why?
Because five has only two factors.
One and itself.
Similarly, if the value of an is seven, the output should be prime.
There was seven is also a prime number one and seven are two factors.
If the value of an is let's say it so it is not prime to the output.
Should be Nordström.
It has many factors.
One, two, four and eight.
More than two factors means that number is not prime.
OK.
So what should be our approach?
So we are going to follow very simple approach, the limited approach.
What will I do starting from the tail end minus one?
So when and and these are going to be the factors.
So what will I do?
My approach will be if I'm able to find any factor, any number between two, two and minus one that
divides and then the number is not prime.
OK.
I am repeating myself.
So my logic will be I have to find one number, just one number from two, two and minus one.
If I am able to find just one single number that divides and then the number is nought prime.
Otherwise, the number is prime.
I'll get one more time.
So we have to find the numbers prime or not.
So I'm telling you one and and these two are going to be the.
There must.
They are going to be defectors.
What is my approach?
Starting from two to N minus one.
I have to just find a single number.
That divides.
And if I'm able to find just one single number that divides and then my work is over and the number
is not prime.
And if I'm not able to find any number from two, two and minus one, that means the number is prime.
OK, so what will I do?
I will simply run a loop.
I will take a variable I which is initialize to two and I will run a loop.
So how many times this loop will work?
Then I is less than goes to N minus one.
So I will take a variable eye, which is Orange likes to do, and I will run this loop till I is left
and it goes to minus one.
And at each time inside the loop, I will try to divide the number.
By eye, if the number is divisible by eye, that means the number is.
Nart Prime.
And if the loop terminates, if the loop terminated, that means I have tried all the numbers starting
from two to end, minus one.
But I am not able to find even a single number that divides.
And so that means if the loop steamier, that the number is prime.
OK.
So let us revise.
So the question was we given and I have to check whether the number is prime or not.
So we're not prime numbers, prime numbers must those number.
That has only two factors.
One and itself.
For example, if the elephant is five, then the most plan, because it has only two factors, one and
five.
Similarly, the value of an is it therefore factors more than two factors means that the number is not
prime.
Okay, so I told you one is going to be a factor and is also going to effector.
So my approach is very simple.
I have to find just one single number.
Starting from two, two and minus one.
If I'm able to find just one single number that divides and then the number is not prime.
And if I'm not able to find just one single number.
And the number is prime.
So I will take a variable eye which is initialized to two and I will run this loop till I is less than
close to and minus one.
And inside the loop, I will try to divide the number by I.
And if the numbers do so by then, the number is not brime.
Otherwise, if this loop terminates.
That means the number is Priam because I have tested all the numbers.
Starting from Doodoo and minus one.
But I am not able to find just one single mother divides them.
So that automatically means if the loop the minutes and the number is blank ocular destroy the flowchart
for the same.
So I will start my program.
That was Embley at Reed and.
OK, sorry, sorry, sorry.
I did something wrong.
Oh, man.
There's a mistake here.
So mistake is.
I was the first to read in so many guys for the mistake.
I don't know, Ohio, I go forward.
So first you have to read and hear.
Sorry again.
And similarly, you have to read and hear first.
Rude.
And so any guys for the mistake?
So you have to first read.
And after reading, then you will take a variable eye, which is initialized to two.
And we have to paste all the numbers from door to and minus and Sawtell I less than Anglo's to end minus
one.
I have to check.
I have to find just one single number that divides.
And so I'm checking if this condition is true.
I will come here.
Is and there is evil.
By I if the numbers do well, by I, that means if this condition is true, that means my number is
not prime, not from.
And I will add and my program so and.
And if this condition is false.
I will go on the radio if I buy one.
And then I will try to do it again.
Simple.
So this loop will check for close to two and minus one.
So this loop Willdan from close to two and Magnuson's and right.
If the condition is false.
If this condition is was that means I have tested all the numbers from two, two and minus one.
But I am not able to find just one single number that divides.
And so that automatically means that the number is plain print.
Prime and then I will end my program.
OK, so there doesn't waste anywhere.
We have done.
So given then we have to check with our number is prime or not.
For example, five is prime because there are two factors.
Only one in five.
Seven is prime.
Because there are only two factors.
One in seven.
It is not right.
Because there are more than two factors.
So the approach is very simple.
I will try to divide by two now and I will do it by two.
Then I will try to divide my four, five, six.
And keep going until and minus one.
If I may.
Well to find just one single number in these set of numbers that divides and that means the number is
not prime.
Otherwise the number is prime.
So the logic of it is simple.
So let us take an example into the value of and is three.
So you start the program, right?
Then I go to and the value of and minus one is.
Do so is to less and close to do for the condition is true, you will come here is endives it by eight
is too divisible by two.
No, you will come here and go under hell if I Bevan's Venezuela if I is three.
Is three less than I to do, so the condition is false.
You will come here.
And they will print prime and then you will end your program.
So the output will be prime, which is hundred percent correct.
Let's say an equals six to a program.
Then I equals two and minus one is five.
So Jack is two less than it was to face.
The condition is true.
You will come here is six Loserville by two.
So the condition is true.
You will come here, Brent, not brain.
So in our primary we printed.
And then you will endure program six is not a prime number.
And I would put this hundred percent.
OK, so let us all.
And the problem and this problem is the last problem for part one.
So problem number three.
Last problem of Paduan.
So the Brownlow's statement is given an.
Fined.
Some of.
Even numbers.
From one to end.
For example, let's say the valley of and is six.
So your answer should be two plus four plus six.
Now, this plan, let's say the value of N is five.
So large butchered.
Your answer should be two plus four.
That is six later and includes two.
So her answer should be to.
So given then we have to find some of even numbers from one to end.
So for inequal six that here numbers from one, two, six, two, four and six.
So add them all.
Similarly, four and five for there only two numbers.
Their daddy went from one to five, two and four.
Add them.
And frankly, two days, only one even number.
So the answer is two.
So how can we solve this problem?
So we need a loop here.
So my loop will start from my crystal and this loop will run for how many times till I.
Is less than 10.
And each time I will increment the value of I buy to why I'm giving the value if I buy two.
Because we only have to consider even numbers.
That's why I initialized my variable with two only, which is the first to a number from one to and.
OK.
So I will initialize the value if I do.
Which is the first two and number from one to N and I will run a loop.
And in the loop, I will do some work.
And then I will increment the value of FI by two because we have to only consider even numbers.
What is the work that I will do in the loop, but I will take a variable sum and I will initialize them
to zero and in the loop, I will add I to the current sum.
And I do the current sum that is no value of some as equals to oil value of some plus I.
So this is the work that I will do in the loop.
And then when my loop will break my on several being some, I will pinch some.
It is the flowchart.
You will get a better understanding from the.
So we start our flowchart.
And then I have duded and.
Then I will take a very well I, which is initialized to two and some, which is initially zero.
And this little children, Bill I.
Is less than equal to.
And so if this condition is true.
What do they do?
Add to the current sum for the new value of some as it goes to all the value of them plus eight.
So this is all value.
And this is the new value of some.
And then I am doing good, wonder if I buy it, or so I will say, blessed because we have to only consider
it unanimous.
And then I will check my condition again.
If this condition is false, I will print.
Some.
And then I will add my program.
OK.
So let's see how this flowchart will work.
Let's say, and it calls for a strike program I read and initially, I quote to some equals zero.
So is do less than I guess to force their condition is true.
You will come here.
And it was a cut in some.
So some IT quotes do.
And then I have to go the value, if I buy do so, I become four.
And then you will come here.
So is for less dingoes to fall, so the condition is true, you will come here and I do the gotten in
some sort of some way become four.
And then in Gurinder earlier fibre to the new earlier fire is six.
Then you will again check the condition.
It takes less than a glitch to win.
No.
So you will come here.
Sorry I did something wrong.
It should be blessed also.
So six here is this condition is false.
You will come here, bring some to six will be printed and then you will end your program.
And six is done.
So for unequals for.
OK, so let's say and equals five.
So you start the program, do you then?
Initially I will do some because it'll.
Some equals zero.
I equals two initially.
Then your check is too less than it goes to five.
So the condition is true.
You will come here.
Add to the current, some sort of sum equals oil value of some plus A, which is to OK.
Now we have to and given the way I live, if I were to see the new value, if I is for Jack, the condition
again is for less than it was two five.
So the condition is true.
You will again come here.
So the new really awesome as it goes to all the value of some place A.
That is six.
Then you have to increment the value of five for the new value, if I will become six.
And then you will check the condition again.
So is six less than Noriko's to five.
So the condition is false.
You will come here.
You will print some.
So six will be printed and then you will end endure program.
You can consider this problem a little bit tough for beginner.
So let us revise what we have done.
So the problem was given and we have to find the sum of even numbers from one to N, for example, and
it was six.
So the output should be two plus four plus six.
At this point, if any of those five are butchered, we tubeless for those six as immediately Franklin's
Truthout, which should be two.
So what do I do?
I will take a very delay and will initialize the variable with two, because two is the first even number
from one to N, and then I will run the loop in the loop.
I will be doing some work and this loop will run until I is less than or equal to doing so.
Less than articles to an annual increment of value if I may do, because I have to only consider even
number.
And what is the Vogtle we're doing here?
Simply, I will take a variable sum.
So the new value of some will be the oil value of some.
Bless a.
And finally, my son will be storing my answer.
And here you can see the diagram.
OK, let's run another example, let's say.
And it goes, do you start the program?
Read and I quote, to sum it goes, it'll sum it close to zero.
I equals to.
Is to less than English to do for the condition is true, you will come here some equals, some blasé.
Some equals some.
Plus I this, too, okay?
Incremental value, if I were to show the new value of ideas for.
And then you will check the condition again.
So is for less than articles to do.
So the condition is false.
You will come here.
You will print some.
And then you will learn to program for animals, too.
My answer is to which is hundred percent correct.