It works for all number except 2. What is wrong, why it says that 2 is not prime??? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

It works for all number except 2. What is wrong, why it says that 2 is not prime???

https://code.sololearn.com/cHiFeTlW5HEN/?ref=app

12th Aug 2019, 11:47 AM
Atin
Atin - avatar
3 Antworten
+ 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
12th Aug 2019, 11:58 AM
Shadow
Shadow - avatar
+ 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
12th Aug 2019, 12:02 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Thank you
13th Aug 2019, 6:32 AM
Atin
Atin - avatar