Ken H

Ken H

  • NA
  • 646
  • 360.6k

Ask the prime number algorithm

Mar 6 2012 2:47 AM
hi all,
  Algorithm code as follows:    

#include <stdio.h>
#include <math.h>
bool PrimeNumber(int n);
void main()
{ int a;
  printf("%s","please input one integer:");
  scanf("%d",&a);
  if(PrimeNumber(a)) printf("%d%s\n",a," is a prime number.");
  else printf("%d%s\n",a," not prime number.");
}

bool PrimeNumber(int n)
{  bool flag=false;
   int k=(int)sqrt(n),i;

 for(i=2;i<=k;i++)
 {
  if(n%i==0)
      flag=false;
     else
      flag=true;
      break;
 }
 return flag;
}

Now the question is When a user enter 2 or 3 to show why not prime number.Other numbers can be displayed correctly.

thanks.


Answers (2)