Search code examples
pythonprimes

python prime number query


number_to_check=int(input("Enter the number you want to check for prime:"))
a= 2         
while number_to_check != a :
    if number_to_check % a == 0:
        a+=1
        print("Number not prime ")
        break
    if number_to_check % a != 0:
        a+=1
        print("Number prime")
        break
if number_to_check =2:
    print("2 not prime")

I can't see a problem or logic error in my code but the code is working incorrectly.


Solution

  • There are better ways to do this, but this follows your philosophy:

    number_to_check=int(input("Enter the number you want to check for prime:"))
    if number_to_check == 2:
        print("2 is prime")
    else:
        for a in range(2, number_to_check//2):
            if number_to_check % a == 0:
                print("Number not prime ")
                break
        else:
            print("Number prime")