Recursive program for prime number Last Updated : 27 Jan, 2022 Comments Improve Suggest changes 27 Likes Like Report 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++ // 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 Program int main() { int n = 15; if (isPrime(n)) cout << "Yes"; else cout << "No"; return 0; } Java // java Program to find whether a Number // is Prime or Not using Recursion import 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. Python3 # 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 Program n = 15 if (isPrime(n)): print("Yes") else: print("No") # This code is contributed by # Smitha Dinesh Semwal C# // C# Program to find whether a Number // is Prime or Not using Recursion using 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 // 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. ?> JavaScript <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> Output: No Comment S Shahnawaz_Ali Follow 27 Improve S Shahnawaz_Ali Follow 27 Improve Article Tags : Misc Mathematical Recursion DSA Prime Number +1 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like