Please explain line no. 17 in the program?

why do we need to write (i==n)???
what does it mean by this??
we have given the initial value to i is 2…y???
please explain with example

@Anku47
lets say you want to check whether 17 is prime or not,
so you check all numbers from 2 to 16(that is n-1) , if any number divides 17, then its not a prime, else it is a prime.
Also if you don’t find any divisor then variable i will be equal to n, hence you know that it is a prime now.
If you still have any doubt, feel free to ask more.

as we have given stopping condition to loop that is n-1 then how will i become equal to n(i==n)???

Because when i=n, only then the condition is violated, so when loop is exited, then i==n or a divisor was found in between.

1 Like

is it means the loop will get terminated only when i=n, despite we have given it stopping condition i<=n
???

see the loop continues till the condition is satisfied,
so when you provide condition as i<n, the loop continues till i=n-1,
as soon as i becomes n, the loop is exited.

so when we come out of loop, i has a value of n(if no divisor was found)

for eg. no. is 7 and the loop has checked from 2 to 6 that no divisor is available so why the no.7 is automatically a prime…then why do we need to write this condition (i==n)??? what will it chech???

because this is the definition of a prime number.

it tells you that no divisor was found and n is prime.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.