# Java Program to Display Alternate Prime Numbers

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.

1. public class AlternatePrimeNumberExample1
2. {
3. //main method
4. public static void main(String[] args)
5. {
6. int num = 20;
7. //prints alternate prime numbers
8. System.out.print(“Alternate prime numbers up to ” + num+” are: “);
9. //method calling
11. }
12. //method checks that the number is prime or not
13. static int checkPrime(int num)
14. {
15. int i, flag = 0;
16. // we have started i=2 because 2 is a known prime number
17. for(i = 2; i<= num / 2; i++)
18. {
19. if(num % i == 0)
20. {
21. flag = 1;
22. break;
23. }
24. }
25. if(flag == 0)
26. return 1;
27. else
28. return 0;
29. }
30. //method for printing alternate prime numbers
31. static void printAlternatePrimeNumber(int n)
32. {
33. //if the value of temp variable is odd, we do not print the prime number
34. //if the value of temp variable is even, we print the prime number
35. int temp = 2;
36. for(int num = 2; num <= n-1; num++)
37. {
38. //checking the number is prime or not
39. if (checkPrime(num) == 1)
40. {
41. //prints the prime number if temp is even
42. if (temp % 2 == 0)
43. System.out.print(num + ” “);
44. //increments the temp variable by 1
45. temp ++;
46. }
47. }
48. }
49. }

Output:

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

Let’s see another logic to display the alternate prime numbers.

1. import java.util.*;
2. public class AlternatePrimeNumberExample2
3. {
4. public static void main(String args[])
5. {
6. int n;
7. int status = 1;
8. int num = 3;
9. //object of the ArrayList class
10. List<Integer> primelist=new ArrayList<Integer>();
11. //object of the Scanner class
12. Scanner scanner = new Scanner(System.in);
13. System.out.print(“Enter the value of n: “);
14. //reading the value of n from the user
15. n = scanner.nextInt();
16. if (n >= 1)
17. {
18. System.out.println(“First “+n+” prime numbers are: “);
19. //2 is a known prime number
20. System.out.println(2);
21. //adding 2 in the ArrayList
23. }
24. for(int i = 2 ; i <=n ; )
25. {
26. for(int j = 2 ; j <= Math.sqrt(num) ; j++)
27. {
28. if(num%j == 0)
29. {
30. status = 0;
31. break;
32. }
33. }
34. if(status != 0)
35. {
36. System.out.println(num);
37. //adding numbers to the ArrayList
39. i++;
40. }
41. status = 1;
42. num++;
43. }
44. System.out.println(“Alternate prime numbers are:”);
45. //loop for printing the alternate prime numbers
46. for(int k=0;k<primelist.size();k++)
47. {
48. if((k%2)==0)
49. System.out.println(“”+primelist.get(k));
50. }
51. }
52. }

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```