Java Program to Check nth Prime Number

In the previous article, we have discussed Java Program to Check Twisted Prime Number

In this article we are going to see how we can print nth prime numbers in Java language.

Program to Check nth Prime Number

Prime numbers are the numbers which divisible by 1 and the number itself.

Example-

3rd prime number is 5
15th prime number is 47
27th prime number is 103

Let’s see different ways to check nth prime number.

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Approach :

  1. Enter/declare nth value and store it.
  2. Then keep checking prime number and keep a track of it.

Method-1: Java Program to Check nth Prime Number By Using Static Value

import java.util.Scanner; 

public class NthPrimeNumberExample   
{  
    public static void main(String[] args)   
    {  
        //nth value declared  
        int n = 27;   
        int number=1, count=0, i; 
        
        while (count < n)  
        {  
            number=number+1;  
            for (i = 2; i <= number; i++)  
            {   
                if (number % i == 0)   
                {  
                    break;  
                }  
            }  
            //means prime as divisible by 1
            //and divisible by 'i'
            //where 'i' is the number itself after completion of for loop
            if (i == number)  
            {  
                //incrementing count value
                count = count+1;  
            }  
        }  
        //prints the nth prime number  
        System.out.println(n +"th prime numberb : " + number);  
    }  
}
Output:

27th prime numberb : 103

Method-2: Java Program to Check nth Prime Number By User Input Value

import java.util.Scanner; 

public class NthPrimeNumberExample   
{  
    public static void main(String[] args)   
    {  
        //Scanner class object created 
        Scanner sc = new Scanner(System.in);
        //asking the user to input 'n' value
        System.out.print("Enter the value of n : ");  
        int n = sc.nextInt();   
        int number=1, count=0, i; 
        
        while (count < n)  
        {  
            number=number+1;  
            for (i = 2; i <= number; i++)  
            {   
                if (number % i == 0)   
                {  
                    break;  
                }  
            }  
            
            //means prime as divisible by 1
            //and divisible by 'i'
            //where 'i' is the number itself after completion of for loop
            if (i == number)  
            {  
                //incrementing count value
                count = count+1;  
            }  
        }  
        //prints the nth prime number  
        System.out.println(n +"th prime numberb : " + number);  
    }  
}
Output:

Enter the value of n : 5
5th prime number : 11

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Programs: