VC++ 质数问题

来源:百度知道 编辑:UC知道 时间:2024/07/08 17:45:13
1. 编写一个函数,确定一个数是否为质数。在程序中调用该函数来确定并打印1-1000之间的所有质数。

#include<iostream.h>
#include<math.h>
void fun(int m)
{
int temp=int(sqrt(m));
int help;
for(help=2;help<=temp;help++)
if(m%help==0)
{
break;
}
if(help>temp)
{
cout<<m<<'\t';
}
}
int main()
{
for(int m=1;m<=1000;m++)
fun(m);
cout<<endl;
return 0;
}