Answer to Question #22373 in C++ for emmanuel

Question #22373
program to find the sum of 1st 10 prime numbers.
1
Expert's answer
2013-01-23T10:54:32-0500
#include<iostream>
#include<math.h>

using namespace std;

int isPrime(unsigned int p)
{
if (p<2)return false; // 0 and 1 are not prime
if (p==2)return true; // 2 is a prime
floats=sqrt(p);
for(unsignedint i=2; i<=s; i++)
{
// if p isdivisible by i, then it is not prime
if (p % i== 0) return false;
}
// p is notdivisible by any number from 2 to sqrt(p), so it is prime
return true;
}


int main()
{
unsigned intn=10; // number of primes
unsigned intsum=0; // sum of primes
unsigned intp=2;
for(unsignedint i=0; i<n; i++)
{
// look forthe next prime
while(!isPrime(p)) {p++;};
// cout<< p << endl;
sum+=p;
p++;
}
cout <<"Sum of first " << n << " primes is " <<
sum << endl;
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS