# program for prime number in java

#### Learn Java Programming for Beginners

Prime number is number which is divisible by 1 and itself.for example Number 7 is prime number because it is divisible by 1 and 7 only.whereas 8 is divisible by 1,2,4,8 therefore 8 is not prime number.

class P

{

public static void main(String args[])

{

int i,prime_number;

DataInputStream in=new DataInputStream(System.in);

System.out.print(" Enter Number ");

prime_number = Integer.parseInt(in.readLine()); //input to check prime number

System.out.print(" The Number "+prime_number+" is ");

i=2;

while(i<=prime_number-1)

{

if(prime_number%i==0)

{

System.out.print("Not a prime Number.");

break;

}

i++;

}

if(i==prime_number)

System.out.print(" Prime Number.");

}

}

program for prime number in java OUTPUT:

Enter Number : 23

The Number 23 is Prime Number.

this is the question for pupil...:-)

1. You have to know that it is not necessary to check to 23, rather you have to check to sqrt(23) i.e. to 4. This is simple mathematics.

2. You have to replace while(i<=prime_number-1) with:

int limit = Math.sqrt(prime_number);

while(i<=limit)

3. That is all...:-)