Recursive program for prime number
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; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 = 15if (isPrime(n)): print("Yes") else: print("No") # This code is contributed by # Smitha Dinesh Semwal |
chevron_right
filter_none
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 |
chevron_right
filter_none
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. ?> |
chevron_right
filter_none
Output:
No
Recommended Posts:
- Recursive sum of digits of a number is prime or not
- Recursive program to check if number is palindrome or not
- C Program to Check Whether a Number is Prime or not
- Python program to check whether a number is Prime or not
- Efficient program to print all prime factors of a given number
- C Program for efficiently print all prime factors of a given number
- Java Program for efficiently print all prime factors of a given number
- Print the nearest prime number formed by adding prime numbers to N
- Find coordinates of a prime number in a Prime Spiral
- Check if a prime number can be expressed as sum of two Prime Numbers
- Recursive program to print all subsets with given sum
- Recursive Program for Binary to Decimal
- Recursive program to generate power set
- Recursive program to print formula for GCD of n integers
- Program to reverse a string (Iterative and Recursive)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



