In: Computer Science
USING PYTHON ONLY
Part 3a: Prime Number Finder
Write a program that prompts the user to enter in a postive number. Only accept positive numbers - if the user supplies a negative number or zero you should re-prompt them.
Next, determine if the given number is a prime number. A prime number is a number that has no positive divisors other than 1 and itself. For example, 5 is prime because the only numbers that evenly divide into 5 are 1 and 5. 6, however, is not prime because 1, 2, 3 and 6 are all divisors of 6.
Here's a sample running of the program:
Enter a positive number to test: 5 2 is NOT a divisor of 5 ... continuing 3 is NOT a divisor of 5 ... continuing 4 is NOT a divisor of 5 ... continuing 5 is a prime number!
And here's another running:
Enter a positive number to test: 9 2 is NOT a divisor of 9 ... continuing 3 is a divisor of 9 ... stopping 9 is not a prime number.
Some notes on your program:
Code and output
Code for copying
n=int(input("Enter a positive number to test: "))
while n<=0:
n=int(input("Enter a positive number to test: "))
count=2
for i in range(2,n):
if n%i==0:
print("{} is a divsor of {} ....stopping".format(i,n))
print(" ")
print("{} is not a prime number.".format(n))
break
if n%i !=0:
print("{} is a NOT a divsor of {}
....continuing".format(i,n))
count+=1
if count == n:
print(" ")
print("{} is a prime number!".format(n))
Code snippet
n=int(input("Enter a positive number to test: "))
while n<=0:
n=int(input("Enter a positive number to test: "))
count=2
for i in range(2,n):
if n%i==0:
print("{} is a divsor of {} ....stopping".format(i,n))
print(" ")
print("{} is not a prime number.".format(n))
break
if n%i !=0:
print("{} is a NOT a divsor of {} ....continuing".format(i,n))
count+=1
if count == n:
print(" ")
print("{} is a prime number!".format(n))