In this section, we will learn how to create a Java program to display alternate prime numbers.
Prime Number: A prime number is a number p such that whenever p divides ab, then either p divides a or p divides b. In other words, a number that is divisible by itself only is called a prime number. For example, 2, 3, 5, 7, etc. are prime numbers.
In the following program, we have defined two methods named checkPrime() and printAlternatePrimeNumber().
The checkPrime() method checks the number (that we have passed as an argument) is prime or not. It returns 1 if the number is prime, else returns 0.
The printAlternatePrimeNumber() method prints the alternate prime numbers up to n.
AlternatePrimeNumberExample1.java
Output:
Let's see another logic to display the alternate prime numbers.
AlternatePrimeNumberExample2.java
Output: