Prime Number Series Java Program

Prime Number Series Program



The series of Prime Numbers all the Prime numbers which are less then the number provided by the user.

The Java Code starts by asking the user to enter a number, and then displays all the Prime Numbers which are less then or equal to that number. This program generates all the prime numbers less then a number, so it is a series of Prime Numbers.

The Java Code is provided below:-

///////////////////////////////////////

package developer;

import java.util.Scanner;

public class PrimeNumberSeries {

public static void main(String[] args)
{ 
int numero = 0;
System.out.println("Enter a number:");
Scanner takeValue = new Scanner(System.in); 
if(takeValue.hasNextInt())
{
numero = takeValue.nextInt();
} 
System.out.println("All the Prime Numbers, which are less then or equal to the number entered are:");
checkPrimeNumber(numero);
}

static void checkPrimeNumber(int val)
{
int tapper = 0; 

for (int i=2; i < val; i++ ){
if(val%i == 0)
{ 
tapper = 0;
break;
} 
else
{
tapper = 1;
}
}
if(tapper == 1 || val == 2)
{
System.out.print(val+" ");
if(val > 2)
checkPrimeNumber(val-1); 
}
else
{
if(val > 2)
checkPrimeNumber(val-1);
else
System.out.println("Number out of range"); 
} 
}
} 

///////////////////////////////////////////////