Monday, August 17, 2009

Prime Number Series Java Program

Hi friends, welcome back to Java Code Online. Today I will be discussing the Java Code to generate a series of Prime Numbers. 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");
}
}
}

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

I hope the Java Code was helpful to you all. Keep buzzing Java Code Online, for more info on Java.

Related Java Programs
Prime Number program in Java
Largest Prime number Java Program
Smallest Prime Number Java Program

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.