# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Find the prime numbers upto MAX_SIZE using Sieve of Eratosthenes.
• Store all primes in a vector.
• For a given number N, return element at (N-1)th index in a vector.

Below is the implementation of the above approach:

## C++

 `// CPP program to the nth prime number  ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// initializing the max value  ` `#define MAX_SIZE 1000005 ` ` `  `// Function to generate N prime numbers using  ` `// Sieve of Eratosthenes ` `void` `SieveOfEratosthenes(vector<``int``> &primes)  ` `{  ` `    ``// Create a boolean array "IsPrime[0..MAX_SIZE]" and  ` `    ``// initialize all entries it as true. A value in  ` `    ``// IsPrime[i] will finally be false if i is  ` `    ``// Not a IsPrime, else true.  ` `    ``bool` `IsPrime[MAX_SIZE];  ` `    ``memset``(IsPrime, ``true``, ``sizeof``(IsPrime));  ` `   `  `    ``for` `(``int` `p = 2; p * p < MAX_SIZE; p++)  ` `    ``{  ` `        ``// If IsPrime[p] is not changed, then it is a prime  ` `        ``if` `(IsPrime[p] == ``true``)  ` `        ``{  ` `            ``// Update all multiples of p greater than or   ` `            ``// equal to the square of it  ` `            ``// numbers which are multiple of p and are  ` `            ``// less than p^2 are already been marked.   ` `            ``for` `(``int` `i = p * p; i <  MAX_SIZE; i += p)  ` `                ``IsPrime[i] = ``false``;  ` `        ``}  ` `    ``}  ` `   `  `    ``// Store all prime numbers  ` `    ``for` `(``int` `p = 2; p < MAX_SIZE; p++)  ` `       ``if` `(IsPrime[p])  ` `            ``primes.push_back(p); ` `            `  `}  ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// To store all prime numbers ` `    ``vector<``int``> primes; ` `     `  `    ``// Function call ` `    ``SieveOfEratosthenes(primes); ` ` `  `    ``cout << ``"5th prime number is "` `<< primes << endl; ` `    ``cout << ``"16th prime number is "` `<< primes << endl; ` `    ``cout << ``"1049th prime number is "` `<< primes; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to the nth prime number   ` `import` `java.util.ArrayList; ` `class` `GFG ` `{ ` `     `  `    ``// initializing the max value  ` `    ``static` `int` `MAX_SIZE = ``1000005``; ` `     `  `    ``// To store all prime numbers ` `    ``static` `ArrayList primes = ` `       ``new` `ArrayList(); ` `     `  `    ``// Function to generate N prime numbers  ` `    ``// using Sieve of Eratosthenes ` `    ``static` `void` `SieveOfEratosthenes()  ` `    ``{  ` `        ``// Create a boolean array "IsPrime[0..MAX_SIZE]"  ` `        ``// and initialize all entries it as true.  ` `        ``// A value in IsPrime[i] will finally be false  ` `        ``// if i is Not a IsPrime, else true.  ` `        ``boolean` `[] IsPrime = ``new` `boolean``[MAX_SIZE];  ` `         `  `        ``for``(``int` `i = ``0``; i < MAX_SIZE; i++) ` `            ``IsPrime[i] = ``true``; ` `         `  `        ``for` `(``int` `p = ``2``; p * p < MAX_SIZE; p++)  ` `        ``{  ` `            ``// If IsPrime[p] is not changed,  ` `            ``// then it is a prime  ` `            ``if` `(IsPrime[p] == ``true``)  ` `            ``{  ` `                ``// Update all multiples of p greater than or  ` `                ``// equal to the square of it  ` `                ``// numbers which are multiple of p and are  ` `                ``// less than p^2 are already been marked.  ` `                ``for` `(``int` `i = p * p; i < MAX_SIZE; i += p)  ` `                    ``IsPrime[i] = ``false``;  ` `            ``}  ` `        ``}  ` `     `  `        ``// Store all prime numbers  ` `        ``for` `(``int` `p = ``2``; p < MAX_SIZE; p++)  ` `        ``if` `(IsPrime[p] == ``true``)  ` `                ``primes.add(p); ` `    ``}  ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `         `  `        ``// Function call ` `        ``SieveOfEratosthenes(); ` `     `  `        ``System.out.println(``"5th prime number is "` `+  ` `                                    ``primes.get(``4``)); ` `        ``System.out.println(``"16th prime number is "` `+  ` `                                    ``primes.get(``15``)); ` `        ``System.out.println(``"1049th prime number is "` `+  ` `                                    ``primes.get(``1048``)); ` `    ``} ` `} ` ` `  `// This code is contributed by ihritik `

## C#

 `// C# program to the nth prime number  ` `using` `System; ` `using` `System.Collections; ` ` `  `class` `GFG ` `{ ` `     `  `// initializing the max value  ` `static` `int` `MAX_SIZE = 1000005; ` ` `  `// To store all prime numbers ` `static` `ArrayList primes = ``new` `ArrayList(); ` ` `  `// Function to generate N prime numbers using  ` `// Sieve of Eratosthenes ` `static` `void` `SieveOfEratosthenes()  ` `{  ` `    ``// Create a boolean array "IsPrime[0..MAX_SIZE]"  ` `    ``// and initialize all entries it as true.  ` `    ``// A value in IsPrime[i] will finally be false ` `    ``// if i is Not a IsPrime, else true.  ` `    ``bool` `[] IsPrime = ``new` `bool``[MAX_SIZE];  ` `     `  `    ``for``(``int` `i = 0; i < MAX_SIZE; i++) ` `        ``IsPrime[i] = ``true``; ` `     `  `    ``for` `(``int` `p = 2; p * p < MAX_SIZE; p++)  ` `    ``{  ` `        ``// If IsPrime[p] is not changed, ` `        ``// then it is a prime  ` `        ``if` `(IsPrime[p] == ``true``)  ` `        ``{  ` `            ``// Update all multiples of p greater than or  ` `            ``// equal to the square of it  ` `            ``// numbers which are multiple of p and are  ` `            ``// less than p^2 are already been marked.  ` `            ``for` `(``int` `i = p * p; i < MAX_SIZE; i += p)  ` `                ``IsPrime[i] = ``false``;  ` `        ``}  ` `    ``}  ` ` `  `    ``// Store all prime numbers  ` `    ``for` `(``int` `p = 2; p < MAX_SIZE; p++)  ` `    ``if` `(IsPrime[p] == ``true``)  ` `            ``primes.Add(p); ` `}  ` ` `  `// Driver Code ` `public` `static` `void` `Main ()  ` `{ ` `     `  `    ``// Function call ` `    ``SieveOfEratosthenes(); ` ` `  `    ``Console.WriteLine(``"5th prime number is "` `+  ` `                                   ``primes); ` `    ``Console.WriteLine(``"16th prime number is "` `+ ` `                                   ``primes); ` `    ``Console.WriteLine(``"1049th prime number is "` `+ ` `                                   ``primes); ` `} ` `} ` ` `  `// This code is contributed by ihritik `

Output:

```5th prime number is 11
16th prime number is 53
1049th prime number is 8377
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : ihritik

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.