c语言 素数判断

来源:百度知道 编辑:UC知道 时间:2024/06/17 22:35:07
#include <stdio.h>
void function (int *a);
void main ()
{int i,k;
printf("enter a number:\n");
scanf("%d",&i);
function (&i);
if (k==0)
printf("shi su shu");
if (k!=1)
printf("bu shi su shu");
getch();
}
void function (int *a)
{int j,k;
for (j=2;j<*a;j++)
{if (*a%j==0)
k=0;

}
}
一直是输出"bu shi su shu"啊,大家帮看下,怎么该最好呢??

这么改最好:
#include <stdio.h>

int function (int a)
{
int j;
for (j=2;j<a;j++)
if (a%j==0)
return 0;
return 1;
}

void main ()
{
int i,k;
printf("enter a number:\n");
scanf("%d",&i);
if(function(i))
printf("shi su shu");
else printf("bu shi su shu");
getchar();
}