/* Write a Java program to Find whether number is Prime or Not. */

class PrimeNumber
{
    public static void main(String args[])
    {
        int number = Integer.parseInt(args[0]);
        int flag=0;
        for(int i=2; i<number; i++)
    {
        if(number%i==0)
            {
                System.out.println(number+" is not a Prime Number");
                flag = 1;
                break;
            }
        }
        if(flag==0)
        System.out.println(number+" is a Prime Number");
    }
}

 OUTPUT:

Compile: javac PrimeNumber.java

Run Ex1: java PrimeNumber 11
11 is a Prime Number

Run Ex2: java PrimeNumber 15
15 is not a Prime Number

 


Buy Head First Java

Best Book for Java

Beginner's Book