nth prime number in Java
In this post, you will learn how to find the nth prime number in the Java programming language.
A prime number is a whole number greater than 1 whose only factors are 1 and itself, like -2, 3, 5, 7, 11, etc. For example, 17 is a prime number because it is only divisible by 1 and 17. On the other hand, 18 is not a prime number because it is divisible by 2, 3, 6, 9, and the number itself.
Java program to find the nth prime number
In the given Java program, we will find the nth prime number. For this, we are importing the Scanner class to take input from the user. The while 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 starts with the initialization of i by 2 and continues till the value is less than or equal to num.
Each time the condition is valid, it will divide the value of num by i and check if it is equivalent to zero or not. If it's equal to zero, the loop breaks and checks whether or not i is equal to num. If it is, then the value of count is increased by 1, and the condition of the while loop is again checked.
When the while loop ends, we get our final value in the variable num.
// Java program find the nth prime number
import java.util.Scanner;
public class NthPrimeNumber {
public static void main(String[] args) {
int num=1, count=0, i;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n to compute the nth prime number: ");
int n = sc.nextInt();
while (count < n){
num=num+1;
for (i = 2; i <= num; i++){
if (num % i == 0) {
break;
}
}
if ( i == num){
count = count+1;
}
}
System.out.println("The " + n +"th prime number is: " + num);
}
}
Output of the above code 1:
Enter the value of n to compute the nth prime number: 11
The 11th prime number is: 31
Output of the above code 2:
Enter the value of n to compute the nth prime number: 20
The 20th prime number is: 71
Java program to find the nth prime number using Sieve of Eratosthenes
The Sieve of Eratosthenes is a method for finding all primes up to any given limit. It is a simple and ancient algorithm used to find prime numbers. It does so by recognising and marking the multiples of each prime number, beginning with the first prime number, 2. Whenever every one of the multiples of each prime is set apart as not prime, the remaining unmarked numbers are prime numbers.
// Java program to find the nth prime number
import java.util.ArrayList;
public class NthPrimeNum
{
// initializing the max value
static int MAX_SIZE = 100000;
// To store all prime numbers
static ArrayList prime_list =
new ArrayList();
// find the nth prime number using
// Sieve of Eratosthenes
static void SieveOfEratosthenes()
{
// Creating a boolean array CheckPrime
// and initialize all entries it as true.
// the value CheckPrime[i] will be false if i is not a IsPrime
boolean [] CheckPrime = new boolean[MAX_SIZE];
for(int i = 0; i < MAX_SIZE; i++)
CheckPrime[i] = true;
for (int p = 2; p * p < MAX_SIZE; p++)
{
if (CheckPrime[p] == true)
{
for (int i = p * p; i < MAX_SIZE; i += p)
CheckPrime[i] = false;
}
}
// Store all prime numbers
for (int p = 2; p < MAX_SIZE; p++)
if (CheckPrime[p] == true)
prime_list.add(p);
}
// Driver Code
public static void main (String[] args)
{
// Function call
SieveOfEratosthenes();
System.out.println("8th prime number is " +
prime_list.get(7));
System.out.println("22th prime number is " +
prime_list.get(21));
System.out.println("2041th prime number is " +
prime_list.get(2040));
}
}
Output of the above code:
8th prime number is 19
22th prime number is 79
2041th prime number is 17789
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number