Program that checks if a number is prime number

后端 未结 5 2328
情深已故
情深已故 2020-12-12 08:35

Hello I have created this program to check if a number is a prime number. It works but for some reason says that 999 is a prime number. Where is my mistake. It would be grea

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 09:10

    you are checking for the 1st iteration only. Irrespective of whether it is prime or not it exits from the loop since one of the section i.e either 'if' or 'else' will execute causing the loop to break. The logic would be to check for the entire range of numbers from 2 to (number/2) and if it divides the number at some point it will not be a prime number. If the loop exits after iterating over the entire loop then it is a prime number. Hope you will be able to do it now. Thanks!.

提交回复
热议问题