Answer to Question #3329 in C++ for mukunda

Question #3329
Write a program to print the sum of all prime numbers between 1 and 100 using while loop and do while.
1
Expert's answer
2011-07-13T11:05:49-0400
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
bool prime[101];
memset (prime, true, sizeof(prime));

int i = 2;
do {

& int n = 2;
& while (i * n <= 100) {
& prime[i*n] = false;
& ++n;
& }

& ++i;
} while (i <= 100);

i = 1;
int sum = 0;
while (i <= 100) {
& if (prime[i])
& sum += i;
& ++i;
}

cout << "The sum 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
New on Blog
APPROVED BY CLIENTS