#include<iostream>
#include<cmath>
using
namespace
std;
int
prime(
int
n)
{
for
(
int
i=2;i<=
sqrt
(
double
(n));i++)
{
if
(n%i==0)
return
0;
}
return
1;
}
int
main()
{
int
N;
cout<<
"请输入一个数int:"
;
while
(1)
{
cin>>N;
if
(!cin||N<2)
{
cin.clear();
cin.ignore();
//清除当前行
cout<<
"请重新输入!"
;
continue
;
}
break
;
}
int
total=0;
for
(
int
i=2;i<=N;i++)
{
if
(prime(i))
{
cout<<i<<endl;
total++;
}
}
cout<<
"一共有"
<<total<<
"个素数。"
;
system
(
"pause"
);
}
原文:http://1624912549.blog.51cto.com/10534998/1734671