0
It works for all number except 2. What is wrong, why it says that 2 is not prime???
3 Réponses
+ 3
Simply initialize 'a' to 1, that way it would work for 2 as well. However, your logic is flawed anyway, as the program treats all odd numbers as prime.
Right now, you just let the loop run until c = 2, check if the remainder is 0, and based on that one test decide the output. You should, however, test in every iteration if the remainder is 0, and stop checking if it is not.
A working implementation could look like this:
https://code.sololearn.com/cajzEAWnkJsU/?ref=app
+ 1
Atin you have unitialized variable a which by default took value zero and hence it is not working for 2..
just initialise a to -1 and it works
+ 1
Thank you