Program to find the next prime number Last Updated : 27 Jul, 2021 Suggest changes Share 13 Likes Like Report 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, until that variable not equals to true, increment N by 1 in each iteration and check whether it is prime or not.If it is prime then print it and change value of found variable to True. otherwise, iterate the loop until you will get the next prime number. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if n // is prime else returns false bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n%2 == 0 || n%3 == 0) return false; for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } // Function to return the smallest // prime number greater than N int nextPrime(int N) { // Base case if (N <= 1) return 2; int prime = N; bool found = false; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } // Driver code int main() { int N = 3; cout << nextPrime(N); return 0; } Java // Java implementation of the approach class GFG { // Function that returns true if n // is prime else returns false static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to return the smallest // prime number greater than N static int nextPrime(int N) { // Base case if (N <= 1) return 2; int prime = N; boolean found = false; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } // Driver code public static void main (String[] args) { int N = 3; System.out.println(nextPrime(N)); } } // This code is contributed by AnkitRai01 Python3 # Python3 implementation of the approach import math # Function that returns True if n # is prime else returns False def isPrime(n): # Corner cases if(n <= 1): return False if(n <= 3): return True # This is checked so that we can skip # middle five numbers in below loop if(n % 2 == 0 or n % 3 == 0): return False for i in range(5,int(math.sqrt(n) + 1), 6): if(n % i == 0 or n % (i + 2) == 0): return False return True # Function to return the smallest # prime number greater than N def nextPrime(N): # Base case if (N <= 1): return 2 prime = N found = False # Loop continuously until isPrime returns # True for a number greater than n while(not found): prime = prime + 1 if(isPrime(prime) == True): found = True return prime # Driver code N = 3 print(nextPrime(N)) # This code is contributed by Sanjit_Prasad C# // C# implementation of the approach using System; class GFG { // Function that returns true if n // is prime else returns false static bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to return the smallest // prime number greater than N static int nextPrime(int N) { // Base case if (N <= 1) return 2; int prime = N; bool found = false; // Loop continuously until isPrime // returns true for a number // greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } // Driver code public static void Main (String[] args) { int N = 3; Console.WriteLine(nextPrime(N)); } } // This code is contributed by 29AjayKumar JavaScript <script> // Javascript implementation of the approach // Function that returns true if n // is prime else returns false function isPrime(n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n%2 == 0 || n%3 == 0) return false; for (let i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } // Function to return the smallest // prime number greater than N function nextPrime(N) { // Base case if (N <= 1) return 2; let prime = N; let found = false; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } // Driver code let N = 3; document.write(nextPrime(N)); // This code is contributed by Mayank Tyagi </script> Output: 5 Create Quiz N Naman_Garg Follow 13 Article Tags : Mathematical DSA Prime Number 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 15 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 My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like