Tuesday, 10 June 2014

Find whether a number is Prime or not.

import java.util.*;
class Prime
{
  public static void main(String []args)
  {
    int no=0,k=0;
Scanner sc = new Scanner(System.in);
  System.out.println("Enter a number:");
  no = sc.nextInt();
  for(int i=2;i<=no/2;i++)
  {
    if(no%i==0)
    {
      k=1;
      break;
    }
    }
  if(k==0)
    System.out.println("It is Prime");
  else
    System.out.println("It is not prime");
}
}

No comments:

Post a Comment