The number and quality of the first n

A user input by a positive integer N, and then calculate the number of the first N primes.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
 int n,i,j,sum,a;
    cin>>n;
    a = 0;
    i = 2;
    sum=0;
    while(a<n){
        for(j=2;j<=i;j++)
            if(i%j == 0)
                break;
        if(j == i)
        {
            sum += i;
            ++a;
        }
        ++i;
    }
    cout<<sum<<endl;
}
Published 44 original articles · won praise 35 · views 796

Guess you like

Origin blog.csdn.net/huangziguang/article/details/104449917