功能
用到的觀念
CODE
#include<iostream>//header file
#include<iomanip>
#include<cmath>
using namespace std;
using std::setw;
using std::fixed;
using std::setprecision;
#include<string>
using std::string;
using std::getline;
int hcf(int, int);
int main()
{
unsigned n;//n save the number of the end,
int i,j,m=0;//declare the number for the loop
cout<<"enter a number"<<endl;
cin>>n;//輸入存到n
if(n==2)//check whether n is equal to 2
{
cout<<"2";//顯示內容
}
if(n==3)//check whether n is equal to 3
{
cout<<"2"<<endl;//顯示內容
cout<<"3"<<endl;//顯示內容
}
if(n>3)
{
cout<<"2 "<<endl;//顯示內容
cout<<"3 "<<endl;//顯示內容
for(i = 2; i<=n;i++)//從2檢查到n判斷是否為質數
{
for(j=2;j<i-1;j++)
{
if(i%j==0)
{
m=1;
break;
}
else
{
m=2;
}
}
if(m==2)
cout<<i<<endl;
}
}
system("pause");
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.7.59