How to find the nth prime number in Java?

Question: How to find the nth prime number in Java? In this example, I will find/get the nth prime number using java Programming language. For this, I am importing the Scanner class. here simple Given an integer any n number. The Program is to find/get a the Nth prime number. also I give you best find the nth prime number in fastest possible way.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Example: print prime numbers in java

Input : 5
Output : 11

Input : 16
Output : 53

Input : 1049
Output : 8377

How to find the nth prime number in Java?

Importing the scanner class:

import java.util.Scanner;

And then I use this simply Java scanner class to included the input from the data enter using user side.

Scanner sc = new Scanner(;
int n = sc.nextInt();

Java example to find the nth prime number

import java.util.Scanner;
public class Prime {
  public static void main(String[] args) {
    int myno=1, count=0, i;
    Scanner sc = new Scanner(;
    // here  get a user input data and then java print value
    System.out.print("Example Like a Eneter Number: ");
    // get a nth prime number in java	
    int n = sc.nextInt(); 
    while (count < n){
      for (i = 2; i <= myno; i++){
        if (myno % i == 0) {
      if ( i == myno){
        count = count+1;
    System.out.println("Your Value of nth prime is: " + myno);

Steps for understanding the example

Let’s see the steps I did to find the nth prime number in Java:

  1. import the Scanner class which is found in java. util package.
  2. Declaring and initializing some variables.
  3. Using the Scanner class to take input from the user.
  4. The logic of the example:
    while (count < n){
    for (i = 2; i <= num; i++){
    if (num % i == 0) {
    if ( i == num){
    count = count+1;

Understanding the logic of the example

For understanding the main logic of the example you must know how to find whether the number is prime or not in this Best Post – Java Program To Check A Number Is Prime or Not.

This loop continues until the value of the count is less than n. If the condition is true then it will increase the value of num by 1.

The for loop begins with the initialization of i by 2 till the value is less than or equal to num. Every time when the condition is true it will divide the value of num by i and checks if its equal to zero or not.

If it is equal to zero, the loop breaks and checks whether i is equal to num. If it is so then the value of count is increased by 1 and then again checks the condition of while loop.
When the while loop terminates I get our final value in the variable num.


Example Like a Eneter Number: 5
Your Value of nth prime: 11

Program to find the Nth Prime Number

number of prime numbers in a specified range in java

import java.util.ArrayList; 
class PKP 
	static int MAX_SIZE = 1000005; 
	static ArrayList<Integer> primenumbers = 
	new ArrayList<Integer>(); 

	static void SieveOfEratosthenes() 

		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] == true) 
				for (int i = p * p; i < MAX_SIZE; i += p) 
					IsPrime[i] = false; 
		for (int p = 2; p < MAX_SIZE; p++) 
		if (IsPrime[p] == true) 
	public static void main (String[] args) 
		System.out.println("Example Like a 5th prime number is " + 
		System.out.println("Example Like a 16th prime number is " + 
		System.out.println("Example Like a 1049th prime number is " + 

// This source code is contributed by 

Web Programming Tutorials Example with Demo

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about nth prime number in java.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1.   AngularJS Ajax Live search
  2.   Radio Button Checked Validation using Angular Example
  3.   Creating Helper Function in Laravel 6 Example
  4.   Top 10 Advanced Laravel Interview Questions Answers
  5.   Angular 9/8 Material Input Box Examples
  6.   Eloquent Insert Multiple Records in Laravel
  7.   php search engine script for mysql database
  8.   Laravel 6 select distinct and order by Example
  9.   Custom Fancy Custom Scrollbar styling using CSS and Jquery
  10.   Login with Facebook and Twitter