Recursive program for prime number - GeeksforGeeks

27 Jan, 2022
Given a number n, check whether it’s prime number or not using recursion.Examples:  
Input : n = 11
Output : Yes

Input : n = 15
Output : No
 
The idea is based on school method to check for prime numbers. 
C++
Java
Python3
C#
PHP
Javascript
// CPP Program to find whether a Number  // is Prime or Not using Recursion#include <bits/stdc++.h>using namespace std; // Returns true if n is prime, else// return false.// i is current divisor to check.bool isPrime(int n, int i = 2){    // Base cases    if (n <= 2)        return (n == 2) ? true : false;    if (n % i == 0)        return false;    if (i * i > n)        return true;     // Check for next divisor    return isPrime(n, i + 1);} // Driver Programint main(){    int n = 15;    if (isPrime(n))        cout << "Yes";    else        cout << "No";     return 0;}
// java Program to find whether a Number// is Prime or Not using Recursionimport java.util.*; class GFG {     // Returns true if n is prime, else    // return false.    // i is current divisor to check.    static boolean isPrime(int n, int i)    {         // Base cases        if (n <= 2)            return (n == 2) ? true : false;        if (n % i == 0)            return false;        if (i * i > n)            return true;              // Check for next divisor        return isPrime(n, i + 1);    }         // Driver program to test above function    public static void main(String[] args)    {         int n = 15;         if (isPrime(n, 2))            System.out.println("Yes");        else            System.out.println("No");    }} // This code is contributed by Sam007.
# Python 3 Program to find whether# a Number is Prime or Not using# Recursion # Returns true if n is prime, else# return false.# i is current divisor to check.def isPrime(n, i = 2):     # Base cases    if (n <= 2):        return True if(n == 2) else False    if (n % i == 0):        return False    if (i * i > n):        return True     # Check for next divisor    return isPrime(n, i + 1)  # Driver Programn = 15if (isPrime(n)):    print("Yes")else:    print("No")     # This code is contributed by# Smitha Dinesh Semwal
// C# Program to find whether a Number// is Prime or Not using Recursionusing System; class GFG{    // Returns true if n is prime, else    // return false.    // i is current divisor to check.    static bool isPrime(int n, int i)    {         // Base cases        if (n <= 2)            return (n == 2) ? true : false;        if (n % i == 0)            return false;        if (i * i > n)            return true;             // Check for next divisor        return isPrime(n, i + 1);    }              // Driver code    static void Main()    {    int n = 15;         if (isPrime(n, 2))            Console.Write("Yes");        else            Console.Write("No");    }     } // This code is contributed by Sam007
<?php// PHP Program to find whether a Number// is Prime or Not using Recursion // Returns true if n is prime, else// return false.// i is current divisor to check.function isPrime($n, $i = 2){    // Base cases    if ($n <= 2)        return ($n == 2) ? true : false;    if ($n % $i == 0)        return false;    if ($i * $i > $n)        return true;     // Check for next divisor    return isPrime($n, $i + 1);} // Driver Code$n = 15;if (isPrime($n))    echo("Yes");else    echo("No"); // This code is contributed by Ajit.?>
<script> // JavaScript  program to find whether a Number// is Prime or Not using Recursion     // Returns true if n is prime, else    // return false.    // i is current divisor to check.    function isPrime(n, i)    {           // Base cases        if (n <= 2)            return (n == 2) ? true : false;        if (n % i == 0)            return false;        if (i * i > n)            return true;                // Check for next divisor        return isPrime(n, i + 1);    }     // Driver code                let n = 15;           if (isPrime(n, 2))            document.write("Yes");        else            document.write("No"); </script>
No
 
Smitha Dinesh Semwal
jit_t
susmitakundugoaldanga
Admired_Olive
Prime Number
Mathematical
Recursion
Mathematical
Recursion
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Program to find sum of elements in a given array
Program for factorial of a number
Operators in C / C++
Program for Tower of Hanoi
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Sum of the digits of a given number
Backtracking | Introduction