BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Print alternate Prime numbers

By Chaitanya Singh | Filed Under: Java Examples

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:
Java Program to display alternate prime numbers

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 Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap