I'm trying to find out whether a given number is prime or not. For example, when I enter the input as 5, it should output "5 is a prime".
However, I get multiple, conflicting outputs.
This is my code:
num = int(input("Enter the number: "))
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
#print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
# if input number is less than
# or equal to 1, it is not prime
else:
print(num,"is not a prime number")
Output is:
Enter the number: 5
5 is a prime number
5 is a prime number
5 is a prime number
5 is not a prime number
The above output shows that 5 is prime number thrice and that 5 is not a prime number once. What is my mistake?
(2,num). You might soon understand that the range(2,Roundup(sqrt(num)))is also fine :-)