求N以内的素数

一、代码实现

 1 #include <iostream>
 2 using namespace std;
 3 void main()
 4 {
 5     int n;          //n以内的素数
 6     cout<<"Please enter the last number:"<<endl;
 7     cin>>n;
 8     for (int i=2;i<n;i++)
 9     {
10         bool flag=true;
11         for (int j=2;j<i;j++)
12         {
13             if (i%j==0)
14             {
15                 flag=false;
16                 break;
17             }
18         }
19         if(flag==true)
20             cout<<i<<"  ";//输出素数
21     }
22     cout<<endl;
23 }

二、运行演示

原文地址:https://www.cnblogs.com/f59130/p/3307547.html