Java Program to Display Alternate Prime Numbers

In the previous article, we have discussed Java Program to Check a Given Number is Perfect Square

In this article we are going to understand what Alternate Prime number in java is and how we can print Alternate Prime numbers in Java with examples. We also include alternating prime sequence in java, alternating prime number, alternation prime program, alternating prime in java, alternative prime sequence penalty.

Program to Display Alternate Prime Numbers

Alternative prime number in java are prime numbers within range but not all numbers are printed, only the alternate ones.

Let’s see different ways to check to display alternate prime numbers.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.


  1. We ask the user to enter/declare a number then iterate all prime numbers within the range using a user defined method isPrime()
    1. We output the alternating prime by using an iterator which only outputs when the iterator is even.

Method-1: Java Program to Display Alternate Prime Numbers By Using Scanner Class and User Defined Method

import java.util.Scanner;
public class AlternatePrimeNumber{
    public static void main(String args[])
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(;
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        System.out.println("The alternate prime numbers within "+num+" are :");
        int iter = 2;
        for(int i = 2; i<=num;i++)
                //Only prints the alternate prime numbers
                    System.out.print(i+", ");

     //Function to check for prime
    static boolean isPrime(int num)
        int iter = 2;
        boolean flag = true;
        while (num > iter)
            if (num % iter == 0)
                flag = false;
        return flag;


Enter a number : 64
The alternate prime numbers within 64 are :
2, 5, 11, 17, 23, 31, 41, 47, 59,

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Test yourself: 

  1. Write A Java Program To Display Alternate Prime Numbers
  2. Write A java Program To Print Alternate Prime Numbers
  3. What Is Alternate Prime Numbers
  4. How To Print Alternate Prime Numbers In Java

Related Java Programs: