#include
intmain()
{
intn,i,j,t,l;
intsum=0;
printf("请输入一个大于2的整数:");
scanf("%d",&n);
l=n;
for(;n>=2;n--)
{
for(i=2;i { if(n%i!=0)//判断其为素数 t=1; else { t=2; break; } } if(t==1) { sum+=n; //printf("%d",n);//输出1-n之间的所有素数 } } printf("1-%d之间的所有素数和为:%d\n",l,sum); return0; } 扩展资料 C++质数大于等于2不能被它本身和1以外的数整除 #include #include #include usingnamespacestd; constlonglongsize=100000;//修改size的数值以改变最终输出的大小 longlongzhishu[size/2]; voidwork(){//主要程序 zhishu[1]=2; longlongk=2; for(longlongi=3;i<=size;i++){//枚举每个数 boolok=1; for(longlongj=1;j if(i%zhishu[j]==0){ ok=!ok; break; } } if(ok){ zhishu[k]=i; cout<<"count"< k++; } } } intmain(){ freopen("zhishu.out","w",stdout); cout<<"count12"< work(); return0; }
//#include "stdafx.h"//If the vc++6.0, with this line.
#include
using namespace std;
int main(int argc,char *argv[]){
int n,i,s;
cout << "Input n(int n>=0)...\nn=";
if(!(cin >> n) || n<0){
cout << "Input error, exit...\n";
return 0;
}
for(s=0,i=1;i<=n;s+=i,i+=2);
cout << "The result is " << s << endl;
return 0;
}
也可以如下做:
//#include "stdafx.h"//If the vc++6.0, with this line.
#include
using namespace std;
int main(int argc,char *argv[]){
int n;
cout << "Input n(int n>=0)...\nn=";
if(!(cin >> n) || n<0){
cout << "Input error, exit...\n";
return 0;
}
n-=!(n&1);
cout << "The result is " << (n+1)*(n+1)/4 << endl;
return 0;
}