Python Program: Find Prime Numbers in Python | Detailed Examples , Multiple Approaches

In this post we will go through the basic python program to find the prime numbers. We will also run the examples and corresponding output.

Prime Numbers

A number that can be divided by only itself and 1 is called Prime Numbers .
For example :  2,3,5,7,11

Algorithm to Find Prime numbers

First we take input from user or take  a  value .

After that store the value into a variable .

And check the variable is prime or not with using loops and conditions

And at last print the result

Approach 1 : Prime Number using Loops

s=12     # s is a checking value is prime or not 
a=2
tr=True
while a<s:    # this is a loop  we access all elements less than input 
  if s%a==0 and s!=a:   # condition for prime numbers 
    print("not prime no. :",s)
    tr=False
    break   # it break loop 
  a=a+1
if tr==True:   
  print("prime no.:" ,s)

Output:
    not prime no. : 12

Approach 2:  Function to Find Prime Numbers

Now we Use functions for finding the number is prime or not  :

def prime1(n):  # function 
    s = True
    for i in range(2,n): # loop for accessing  and range for 3 t0 10
        if n%i==0 :    # condition
            s=False
            break
    return s
for i in range(3,10):  
    n=i
    print(prime1(n))

Output :
   True
   False
   True
   False
   True
   False
  False


 

More Python Program

 

 

Leave a Comment

%d bloggers like this: