#include<stdio.h>
#include<math.h>
int main()
{
int prime(int m);
int n;
scanf("%d",&n);
if(prime(n)==1)
printf("%d is a prime number\n",n);
else
printf("%d is no a prime number\n",n);
return 0;
}
prime(int m)
{
int j,k,f;
k = sqrt(m);
for(j=2;j<=k;j++)
if(m%j==0) break;
if(j>k)
f = 1;
else
f = 0;
return(f);
}
/*vc++6.0:
5
5 is a prime number
Press any key to continue
6
6 is no a prime number
Press any key to continue
*/
另外一种写法
#include<stdio.h>
#include<math.h>
int prime(int m)
{
int j,k,f;
k = sqrt(m);
for(j=2;j<=k;j++)
if(m%j==0) break;
if(j>k)
f = 1;
else
f = 0;
return(f);
}
int main()
{
int n;
scanf("%d",&n);
if(prime(n)==1)
printf("%d is a prime number\n",n);
else
printf("%d is no a prime number\n",n);
return 0;
}
/*vc++6.0:
5
5 is a prime number
Press any key to continue
6
6 is no a prime number
Press any key to continue
*/