# 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.

Expert's answer

#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;

}

#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;

}

## Comments

## Leave a comment