In this tutorial, we will write a Java program to **display alternate prime numbers upto a given value**.

## Java Example to print alternate prime numbers

In the following example we have two user defined methods: `checkPrime()`

and `printAltPrime()`

.

The **checkPrime()** method checks whether the number passed as an argument is prime or not, if the number is prime, this method returns 1 else it returns false.

The **printAltPrime()** method prints the alternate prime numbers upto the value passed as an argument.

Do read the comments to understand the logic of the program.

class JavaExample { //method for checking prime number static int checkPrime(int num) { int i, flag = 0; for(i = 2; i<= num / 2; i++) { if(num % i == 0) { flag = 1; break; } } /* If flag value is 0 then the given number num * is a prime number else it is not a prime number */ if(flag == 0) return 1; else return 0; } //Method for printing alternate prime numbers static void printAltPrime(int n) { /* When the temp value is odd then we are * not printing the prime number and when it is * even then we are printing it, this way we are * displaying alternate prime numbers */ int temp = 2; for(int num = 2; num <= n-1; num++) { //checking each number whether it is prime or not if (checkPrime(num) == 1) { // if temp is even then only print the prime number if (temp % 2 == 0) System.out.print(num + " "); temp ++; } } } public static void main(String[] args) { int num = 20; System.out.print("Alternate prime numbers upto " + num+" are: "); printAltPrime(num); } }

**Output:**

#### Related Java Examples

1. Java program to display first n prime numbers

2. Java program to check prime number

3. Java program to check if a number is perfect square

4. Java program to check Armstrong number

## Leave a Reply