How to find the nth prime number in Java?

pakainfocom profile image pakainfo ・2 min read

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);

You can visit my Article with More Examples : click here

Posted on by:

pakainfocom profile



My name is Angular King. I'm a full-stack developer, entrepreneur and owner of Pakainfo. I live in India and I love to write


Editor guide