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.

program for prime number in java

program for prime number in java

class P
public static void main(String args[])
int i,prime_number;

DataInputStream in=new DataInputStream(;

System.out.print(” Enter Number “);

prime_number = Integer.parseInt(in.readLine());                                                              //input to check prime number
System.out.print(” The Number “+prime_number+” is “);
System.out.print(“Not a prime Number.”);
System.out.print(” Prime Number.”);


program for prime number in java OUTPUT:

Enter Number : 23

The Number 23 is Prime Number.


Founder of Java Engineering Programs. Follow me on Google+ ,Linked in.

You may also like...

1 Response

  1. Alex says:

    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);
    3. That is all…:-)

Leave a Reply

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