Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Program to find the next prime number
Next article icon

Recursive program for prime number

Last Updated : 27 Jan, 2022
Comments
Improve
Suggest changes
Like Article
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

 

Next Article
Program to find the next prime number

S

Shahnawaz_Ali
Improve
Article Tags :
  • Misc
  • Mathematical
  • Recursion
  • DSA
  • Prime Number
Practice Tags :
  • Mathematical
  • Misc
  • Prime Number
  • Recursion

Similar Reads

  • Program to check for Twin Prime Numbers
    A Twin prime are those numbers which are prime and having a difference of two ( 2 ) between the two prime numbers. In other words, a twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes; an alternative name for this is prime twin or prime
    6 min read
  • Program to find the Nth Prime Number
    Given an integer N. The task is to find the Nth prime number. Examples: Input : 5 Output : 11 Input : 16 Output : 53 Input : 1049 Output : 8377 Approach: Find the prime numbers up to MAX_SIZE using Sieve of Eratosthenes.Store all primes in a vector.For a given number N, return the element at (N-1)th
    13 min read
  • Program to find the next prime number
    Given an integer N. The task is to find the next prime number i.e. the smallest prime number greater than N. Examples: Input: N = 10 Output: 11 11 is the smallest prime number greater than 10. Input: N = 0 Output: 2 Approach: First of all, take a boolean variable found and initialize it to false.Now
    5 min read
  • Stored Procedure for prime numbers in MYSQL
    In this article, you will see how you can write the logic of stored procedure to generate prime numbers for any given input. Title : Given a number N print all the prime number (<=N) separated by comma (, ) using Stored Procedure in MYSQL. Example-1 : Input : N = 10 Output : 2, 3, 5, 7 Example-2
    2 min read
  • Python Program to Check Prime Number
    Given a positive integer, check if the number is prime or not. A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples of first few prime numbers are {2, 3, 5, Examples: Input: n = 11 Output: true Input: n = 15 Output: false Input: n = 1 Output: fal
    2 min read
  • Program to print prime numbers from 1 to N.
    Given a number N, the task is to print the prime numbers from 1 to N.Examples: Input: N = 10Output: 2, 3, 5, 7Explanation : The output "2, 3, 5, 7" for input N = 10 represents the list of the prime numbers less than or equal to 10. Input: N = 5Output: 2, 3, 5 Explanation : The output "2, 3, 5" for i
    15+ min read
  • Prime Factor Program
    Given an integer n, write a program to find and print all the prime factors of n. A prime factor is a prime number that divides n exactly (without leaving a remainder). Examples:Input: n = 36Output: 2 2 3 3Explanation: For n = 36, dividing by 2 twice and 3 twice gives the prime factors 2, 2, 3, 3. I
    8 min read
  • Program to find Prime Fibonacci Numbers till N
    Given a number, find the numbers (smaller than or equal to n) which are both Fibonacci and prime. Examples: Input : n = 40 Output: 2 3 5 13 Explanation : Here, range(upper limit) = 40 Fibonacci series upto n is, 1, 1, 2, 3, 5, 8, 13, 21, 34. Prime numbers in above series = 2, 3, 5, 13. Input : n = 1
    8 min read
  • Prime numbers after prime P with sum S
    Given three numbers sum S, prime P, and N, find all N prime numbers after prime P such that their sum is equal to S.Examples : Input : N = 2, P = 7, S = 28 Output : 11 17 Explanation : 11 and 17 are primes after prime 7 and (11 + 17 = 28) Input : N = 3, P = 2, S = 23 Output : 3 7 13 5 7 11 Explanati
    13 min read
  • Javascript Program for Count Primes in Ranges
    Given a range [L, R], we need to find the count of total numbers of prime numbers in the range [L, R] where 0 <= L <= R < 10000. Consider that there are a large number of queries for different ranges. Examples: Input : Query 1 : L = 1, R = 10 Query 2 : L = 5, R = 10Output : 4 2Explanation :
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences