Objective of this Post- Program of Prime number by recursion in python. Write a recursive function ChkPrime() that checks a number for Prime. This python project is useful for beginners and CBSE KV School Class 11 and Class 12 students computer science practical file and NIELIT O Level Programming and Problem Solving through Python ( Module M3-R5).
Program code screenshot
The source code of python program to generate prime number by recursion is given below. The source code file of this program is present in the last column on this page.
Explanation of Python Code
def ChkPrime(n,i): #function for checking prime number ChkPrime(n,i) n is the number to be found whether it is prime or not. i is the number #from where divisibilty starts
while i<n: #while loop goes on till i<n i is increased by 1 every time
if n%i==0: #if n is divisible by i then it will return false
return False #Returing false
i+=1 # i is increased by one
ChkPrime(n,i) # recursive function
return True #return true if it find indivisible by i
n=int(input(“Enter the number”)) # main program takes integer n as input
if ChkPrime(n,2): # call the recursive function with i=2 value
print(“Number is prime”) # if the function return true number is prime
print(“Number ain’t prime”) #if the function return false
Output of Program of Prime number by recursion in python
Testing of Program
53 is input, to check whether it is prime or not. Its shows 53 as prime. which is correct.
we also have given input as 22 which we know it is not prime , it shows the same.
Source code of Program -> click here
Thanks for reading and learning about this program.
for more python programs -> click here