Java Program to check Prime Number

Prime Number Java Programming:
Any number which is divisible by 1 or itself is known as Prime Number.
For example : 2,5,7 are prime numbers as they are not divisible by any of the numbers except 1 and themselves.

Java Program to check Prime Number

/* Program to Find whether number is Prime or Not by Codext.com */

class PrimeNo{

      public static void main(String args[]){

          int num = Integer.parseInt(args[0]);

         int flag=0;

         for(int i=2;i<num;i++){

             if(num%i==0)

              {

                 System.out.println(num+" is not a Prime Number");

                 flag = 1;

                 break;

              }

         }

         if(flag==0)

             System.out.println(num+" is a Prime Number");

    }

}

Output of Java Program to check whether a number is Prime or not.


0 comments:

Post a Comment