2018年4月14日 星期六

//C++ 找出某範圍的所有質數

//找出到n範圍的所有質數 
#include<iostream>
#include<cstdlib>
using namespace std;
int main(void)
{ //使用者輸入 
int n;
cout << "Please input a number:" ;
cin >> n;

for(int i=2; i<=n; i++)
{
bool factor=false;
//找出質數 
for(int j=2; j<i; j++)
{
if(i%j==0)
factor=true;
}
//若為質數在印出 
if(factor==false)
{
cout << i <<endl;
}
}
system("pause");
return 0;
}









沒有留言:

張貼留言