Write a program to check that the entered no is prime or not.

import java.util.*;
class PrimeNo
 {
  public static void main(String[] args)
  {
   int num;
   int i;
   Scanner in=new Scanner(System.in);
   System.out.println("enter the value of num");
   num=in.nextInt();
   for (i=2; i < num ;i++ )
    {
     int n = num%i;
     if (n==0)
       {
         System.out.println("not Prime!");
         break;
       }
    }
     if(i == num)
      {
       System.out.println("Prime number!");
      }
  }
}

No comments:

Post a Comment