2007-11-10, 11:31 PM
|
#2 (permalink)
|
註冊會員
|
語法:
#include<iostream>
using namespace std;
int main(){
int s=0,n,m,i; //s表總合,n表輸入的數字,m表要找的質數
cout <<"please put in a num";
cin >>n;
if(n==1) cout <<"The sum is 0."<<endl;
else{
for(m=2;m<=n;m++) //m由2到n
for(i=2;i<=m;i++){
if(m%i==0&&i!=m) break; //當m不為質數時跳出
if(i==m){
cout <<m<<endl; //m為質數,s+m=s
s+=m;
}
}
}
cout <<"The sum is "<<s<<"."<<endl;
system("pause");
return 0;
}
還有 The prime numbers between 1 and 100 are
這句有誤,應該是 The prime numbers between 1 and 50 are
|
|
|
送花文章: 15,
收花文章: 18 篇, 收花: 28 次
|