c语言。编写函数。求出1000以内的所以素数

如题所述

提供以下C++,仅供参考。

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
   int i,n;
   cout<<"1~1000间的素数有:"<<endl;
   for(n=2;n<=1000;n++)
   {
                      for(i=2;i<=sqrt(n);i++)
                      if(n%i==0)
                      break;
                      if(i>sqrt(n))
                      cout<<n<<" ";
                      }
                      cout<<endl;
                      //使用VC++6.0编译器的时候不用加getchar();使用VS2010以上的版                       //  本的时候bu加的话有可能会报错。
}

温馨提示:答案为网友推荐,仅供参考