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 initialise 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 untill 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 = 3print(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 |
5
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Quick ways to check for Prime and find next Prime in Java
- Find next palindrome prime
- Find coordinates of a prime number in a Prime Spiral
- Next smallest prime palindrome
- Find Next number having distinct digits from the given number N
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Given a number, find the next smallest palindrome
- Find next greater number with same set of digits
- Find Next Sparse Number
- Find the Next perfect square greater than a given number
- Find the next fibonacci number
- Find the next Non-Fibonacci number
- Program to find the Nth Prime Number
- Prime numbers after prime P with sum S
- Sum of each element raised to (prime-1) % prime
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
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.

