VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-find-the-nth-prime-number/

⇱ Program to find the Nth Prime Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find the Nth Prime Number

Last Updated : 12 Jul, 2025

Given an integer N. The task is to find the Nth prime number.

Examples:  

Input :
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 index in a vector.

Below is the implementation of the above approach: 


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

Another approach : 

  • for finding prime number at given position write a isPrime function to check number is prime or not
  • write a function to get prime number at given position 
     

Below is the implementation of the above approach : 


Output
5th prime number is : 11
7th prime number is : 17
10th prime number is : 29
Comment