#include <stdio.h>
#include <math.h>
int is_prime(int n);
int main(void)
{
int n;
scanf("%d", &n);
if(is_prime(n))
printf("this is a prime.\n");
else
printf("this is not a prime.\n");
return 0;
}
int is_prime(int n)
{
int k, i;
if(n == 1||n <= 0)
return 0;
else if(n == 2)
return 1;
else
{
k = sqrt(n);
for(i = 2;i <= k;i ++)
{
if(n % i == 0)
return 0;
}
return 1;
}
}
----------------解决方案--------------------------------------------------------
这样的错误实在是令在下费解啊.看了半天原来是打错了
----------------解决方案--------------------------------------------------------
#include <iostream.h>
int prime(int x);
void main()
{
int i,a;
cout<<"your number:"<<endl;
cin>>a;
i=prime(a);
if (i==0)
cout<<"this is a prime;"<<endl;
else
cout<<"this is not a prime"<<endl;
}
int prime(int x)
{
int flag;
for(int j=2;j<x;j++)
{
if(x%j!=0)
flag=0;
else
{
flag=1;
break;
}
if(flag==1)
break;
}
if(flag==0)
return(0);
else
return(1);
}
----------------解决方案--------------------------------------------------------
小弟不才,有好的方案望大家多多指点啊
----------------解决方案--------------------------------------------------------