A natural number that is greater than 1 , divided by 1 and itself is called prime number. In this tutorial, we are going to write a program to print Nth prime number in the number series .

import java.util.Scanner;
public class NthPrime {
 
    public boolean isPrimeNumber(int number){
	    if (number == 0 || number==1)
		{
			return false ;
		}
		if (number == 2)
		{
			return true ;
		}
        for(int i=2; i<=number/i; i++){
            if(number % i == 0){
                return false;
            }
        }
        return true;
    }
    public static void main(String args[]){
           NthPrime primenumber = new NthPrime();
		   System.out.print("Please Enter Number: ");
           Scanner read = new Scanner(System.in);
           int number = read.nextInt();
		   int count =0 ;
		   int temp =0 ;
		   while(true)
		   {
		   temp++;
		   if(primenumber.isPrimeNumber(temp))
			   count++;
		   if(count ==number){
			   System.out.println( number+"th Prime is " +temp );
			   break;
		   }
		   }
    }
}

Leave a Reply

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