**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){ myno=myno+1; for (i = 2; i <= myno; i++){ if (myno % i == 0) { break; } } 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:

- import the Scanner class which is found in java. util package.
- Declaring and initializing some variables.
- Using the Scanner class to take input from the user.
- The logic of the example:

while (count < n){ num=num+1; for (i = 2; i <= num; i++){ if (num % i == 0) { break; } } 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.

### Output:

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) primenumbers.add(p); } public static void main (String[] args) { SieveOfEratosthenes(); System.out.println("Example Like a 5th prime number is " + primenumbers.get(4)); System.out.println("Example Like a 16th prime number is " + primenumbers.get(15)); System.out.println("Example Like a 1049th prime number is " + primenumbers.get(1048)); } } // This source code is contributed by pakainfo.com

##### Web Programming Tutorials Example with Demo

### Read :

### Summary

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.