求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 }

 

二、运行演示

posted @ 2013-09-07 16:49  不懂编程的程序员  阅读(255)  评论(0编辑  收藏  举报