In this code even without break code runs fine so what is the need of break here
While loop and break statement
@ikrarrus
No , it doesn’t work properly. Just input any composite number and see for yourself.
Input :
30
Expected Output :
N is not a prime number
Output without the break statement :
N is not a prime numberN is not a prime numberN is not a prime numberN is not a prime numberN is not a prime numberN is not a prime numberN is a prime number
So can you please explain why "N is not prime numberN " get printed five times exactly
@ikrarrus
In case of 30 , 30 has the factors - 1,2,3,5,6,10,30
Since the loop goes from 2 to 29 , 1 and 30 are excluded leaving only 5 factors. The statement “N is not prime number” is printed for all of them and then finally when the loop exits , the statement “N is a prime number” is also printed at the last which is absolutely wrong.
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.