c++ 求2-100内的素数问题

最近在学习c++,从最基础的联系

这也是很常见的问题

 

View Code
#include  " stdafx.h "
#include <iostream>
using  namespace std;

int _tmain( int argc, _TCHAR* argv[])
{
     int j,i;
     int nums= 0; // 质数计数
     int s= 0;    // 质数之和
     for(i= 2;i<= 100;i++) { 
         bool isSushuo= true;
         for(j= 2;j<i;j++){
             if(i%j== 0)
                 break;
        }
         if(j==i){ // 说明比i小的都循环完了
            nums++;
            s+=i;
             if(nums% 7== 0){
                cout<<i<< " \t "<<endl;
            } else{
                cout<<i<< " \t ";
            }

        }
    }
    cout<<endl;
    cout<< " 2-100 所有的质数有: "<<nums<< " "<<endl;
    cout<< " 2-100 所有的质数之和为: "<<s<<endl;
    cin>>s;
     return  0;
}

 

你可能感兴趣的:(C++)