Home » How to find the nth prime number in Java?

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.

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(System.in);
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(System.in);
    // 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.

Read Also:  How to add bootstrap accordion with plus minus Toggle Icons?

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.

Read Also:  PHP upload custom file in ckeditor Example

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 pakainfo.com 

Web Programming Tutorials Example with Demo

Read :

Read Also:  WordPress plugin dependencies on Activation


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 infinityknow.com 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. Read Also:  C# convert Decimal to Binary octal hexadecimal Examples
  2. Read Also:  Restart Server from Linux (Ubuntu)
  3. Read Also:  VueJs Dynamic Class and Style Bindings
  4. Read Also:  PHP MySQLi Database connection select Multiple query
  5. Read Also:  Vue JS Datepicker Examples with Demo
  6. Read Also:  Laravel Increment and Decrement Example
  7. Read Also:  CodeIgniter Beginners Tutorial Step by Step
  8. Read Also:  Restart Server from Linux (Ubuntu)
  9. Read Also:  Vue JS Datepicker Examples with Demo
  10. Read Also:  PHP MySQLi Database connection select Multiple query

Leave a Comment

Your email address will not be published. Required fields are marked *