Python Program to check a number is prime or not using recursion


In this program, you will learn how to check a number is prime or not using recursion in Python.


Some list are: 2 3 5 7 11

Example: How to check a number is prime or not using recursion in Python

p = 1


def check(n, i):
    global p
    if n % i == 0 and i <= n / 2:
        p = 0
        return p
    elif i <= n / 2:
        check(n, i=i + 1)

    return p;


n = int(input("Enter a number:"))

p = check(n, 2)
if p == 1:
    print("Number is prime:", n)
else:
    print("Number is not prime:", n)

Output:

Enter a number:7
Number is prime: 7