Write a Python Program to Find Prime Number using For Loop, While Loop, and Functions. Any natural number that is not divisible by any other except 1 and itself is called a Prime.
The list goes like 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, etc. 2 is the only even num.
Python Program to find Prime Number using For Loop
This program allows the user to enter any integer value and checks whether the given number is a Prime or Not using For Loop.
Number = int(input(" Please Enter any Number: "))
count =Number = int(input("Please Enter any Value: "))
count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break
if (count == 0 and Number != 1):
print(" %d is a Prime" %Number)
else:
print(" %d is Not" %Number)

Within the Python for loop, there is an Python If else statement to check whether the value is divisible by i, exactly equal to 0, or not. If the condition is True, then the Count value is incremented, and then the Python break Statement is executed. Next, we used another if statement to check whether the Count is zero and whether the Num is not equal to 1.
The user entered integer in the above example is 365.
First Iteration: for i in range(2, 365//2). It means, for i in range (2, 182.5) – The condition is True. Now, Check the if condition – if (365%2 == 0). As you know, the condition is False. Next, i become 3
Do the same for the remaining For loop iterations of the Program to find the Prime Number.
Next, it enters into the Python If statement. if(count == 0 && Number != 1 ). In all the above iterations, If the condition failed, the Count Value has not incremented from initialized o. And the one that we used is 365 (not zero). So, the condition is True, which means Prime.
Check prime number using for loop else
This program uses the for-else block to check whether the user-given number is prime.
n = int(input("Enter the Value = "))
if n > 1:
for i in range(2, n):
if n % i == 0:
print(n, "is not a prime number")
break
else:
print(n, "is a prime number")
else:
print(n,"is not a prime number")
Enter the Value = 12
12 is not a prime number
Enter the Value = 11
11 is a prime number
Python Program to find Prime Number using While Loop
This program is the same as the above. We have just replaced the for loop in the above Python program with a Python While loop.
# using While Loop
Number = int(input(" Please Enter any Num: "))
count = 0
i = 2
while(i <= Number//2):
if(Number % i == 0):
count = count + 1
break
i = i + 1
if (count == 0 and Number != 1):
print(" %d is a Prime" %Number)
else:
print(" %d is not" %Number)

Python Program to find Prime Number using Functions
This program is the same as the first example. However, we separated the factor’s logic by defining the new Python Function. Next, the If Else statement checks whether the given number is prime or not.
# using Functions
def prime_number(Number):
count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
return count
Num = int(input(" Please Enter any Num: "))
cnt = prime_number(Num)
if (cnt == 0 and Num != 1):
print(" %d is a Prime" %Num)
else:
print(" %d is not" %Num)

In the following example, a for loop ignores all even numbers from 3 to the square root of the given number. This improves code performance because there is a significant drop in the number of for loop iterations. Refer to the Python sqrt.
import math
def checkPrime(n):
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
count = 0
for i in range(3, int(math.sqrt(n) + 1)):
if n % i == 0:
return False
return True
Num = int(input("Enter any Number: "))
if checkPrime(Num):
print(f"{Num} is a Prime")
else:
print(f"{Num} is not Prime")
Enter any Number: 23
23 is a Prime
Use the isprime() method to check if the number is prime
The SymPy module has a built-in isprime() method that accepts a number and checks whether it is a prime number.
from sympy import isprime
n = 17
print(isprime(n))
True
Program to find a prime number using recursion
In this program, we defined a function where, if the square root of the given number equals 1, it’s a prime number. Next, if the given number is divisible by its square root, it’s not prime. Otherwise, call the function recursively with the decremented i value.
import math
def checkPrime(n, i):
if i == 1:
return True
if n % i == 0:
return False
return checkPrime(n, i - 1)
n = int(input("Enter the Value = "))
print(checkPrime(n, int(math.sqrt(n))))
Enter the Value = 15
False
Enter the Value = 23
True
Related Number Programs