Wednesday, November 18, 2009

c program to find given number is prime or not


main()
{
      printf("Enter a number: ");
     
      int n;
      scanf("%d", &n);
     
      if(n == 2)
           printf("%d is prime", n);
      else if(n % 2 == 0 || n < 2)
           printf("%d is not prime", n);
      else
      {
           int x;
           for(x = 0; x < (int)sqrt((double)n); x++)
                if(n % x == 0)
                {
                     printf("%d is not prime", n);
                     return;
                }
           printf("%d is prime", n);
      }
}

No comments:

Post a Comment