Display Alternate Prime Numbers in Java

In this section, we will explore how to develop a Java program that can exhibit alternate prime numbers.

A prime number is a number, denoted as p, which satisfies the property that if p divides the product of two numbers (a and b), then p must also divide at least one of the two numbers, a or b. Simply put, a prime number is only divisible by itself and 1, making it distinct from other numbers. Examples of prime numbers include 2, 3, 5, 7, and so on.

In the given program, there are two methods called checkPrime() and printAlternatePrimeNumber().

The checkPrime() method is responsible for determining whether a given number (provided as an argument) is a prime number or not. It evaluates the number and returns 1 if it is prime, and 0 if it is not.

On the other hand, the printAlternatePrimeNumber() method is responsible for displaying the alternate prime numbers up to a given value, n. It utilizes the checkPrime() method to identify prime numbers and prints them in an alternating fashion.

AlternatePrimeNumberExample1.java

public class AlternatePrimeNumberExample  
{   
//main method  
public static void main(String[] args)    
{   
int num = 20;   
//prints alternate prime numbers  
System.out.print("Alternate prime numbers up to " + num+" are: ");    
//method calling  
printAlternatePrimeNumber(num);   
}  
//method checks that the number is prime or not  
static int checkPrime(int num)   
{   
int i, flag = 0;   
// we have started i=2 because 2 is a known prime number  
for(i = 2; i<= num / 2; i++)   
{   
if(num % i == 0)   
{   
flag = 1;   
break;   
}   
}   
if(flag == 0)   
return 1;   
else  
return 0;   
}   
//method for printing alternate prime numbers  
static void printAlternatePrimeNumber(int n)   
{   
//if the value of temp variable is odd, we do not print the prime number  
//if the value of temp variable is even, we print the prime number  
int temp = 2;   
for(int num = 2; num <= n-1; num++)   
{   
//checking the number is prime or not  
if (checkPrime(num) == 1)   
{    
//prints the prime number if temp is even  
if (temp % 2 == 0)   
System.out.print(num + " ");   
//increments the temp variable by 1  
temp ++;   
}   
}   
}   
}

Output:

Alternate prime numbers up to 40 are: 2 5 11 17 23 31

AlternatePrimeNumberExample2.java

import java.util.*;  
public class AlternatePrimeNumberExample2  
{  
public static void main(String args[])  
{  
int n;  
int status = 1;  
int num = 3;  
//object of the ArrayList class  
List<Integer> primelist=new ArrayList<Integer>();  
//object of the Scanner class  
Scanner scanner = new Scanner(System.in);  
System.out.print("Enter the value of n: ");  
//reading the value of n from the user  
n = scanner.nextInt();  
if (n >= 1)  
{  
System.out.println("First "+n+" prime numbers are: ");  
//2 is a known prime number  
System.out.println(2);  
//adding 2 in the ArrayList  
primelist.add(2);  
}  
for(int i = 2 ; i <=n ; )  
{  
for(int j = 2 ; j <= Math.sqrt(num) ; j++)  
{  
if(num%j == 0)  
{  
status = 0;  
break;  
}  
}  
if(status != 0)  
{  
System.out.println(num);  
//adding numbers to the ArrayList  
primelist.add(num);  
i++;  
}  
status = 1;  
num++;  
}  
System.out.println("Alternate prime numbers are:");  
//loop for printing the alternate prime numbers  
for(int k=0;k<primelist.size();k++)  
{  
if((k%2)==0)  
System.out.println(""+primelist.get(k));  
}  
}  
}

Output:

Enter the value of n: 10
First 10 prime numbers are: 
2
3
5
7
11
13
17
19
23
29
Alternate prime numbers are:
2
5
11
17
23

For additional insights on ‘Display Alternate Prime Numbers’, consider following the tutorials.freshersnow.com platform.