Home » Java Program to Display Alternate Prime Numbers

Java Program to Display Alternate Prime Numbers

by Online Tutorials Library

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.

AlternatePrimeNumberExample1.java

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.

AlternatePrimeNumberExample2.java

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  

Next TopicJava Programs

You may also like