Question #47753

c++program to print the sum of all prime numbers in an array.

Expert's answer

#include <iostream>

using namespace std;

bool IsPrime(int inputnumber);

//main method

int main()

{

//total number to input

int totalNumber=0;

//array of numbers

int numbers[100];

//promt user to enter "How many numbers do you want to enter"

cout<<"How many numbers do you want to enter?: ";

//read total Number

cin>>totalNumber;

for(int i=0;i<totalNumber;i++){

cout<<"Enter value: ";

cin>>numbers[i];

}

//check if velues in array is prime or not

for(int i=0;i<totalNumber;i++){

if(IsPrime(numbers[i])){

cout<<numbers[i]<<" is prime.\n";

}else{

cout<<numbers[i]<<" is NOT prime.\n";

}

}

//delay

system("pause");

//exit program

return 0;

}

//Check is number is prime ot not

bool IsPrime(int inputnumber){

if ((inputnumber & 1) == 0)

{

//check if number =2

if (inputnumber == 2)

{

return true;

}

else

{

return false;

}

}

//Loop and find if number is prime or not

for (int i = 3; (i * i) <= inputnumber; i += 2)

{

if ((inputnumber % i) == 0)

{

return false;

}

}

return inputnumber != 1;

}

#include <iostream>

using namespace std;

bool IsPrime(int inputnumber);

//main method

int main()

{

//total number to input

int totalNumber=0;

//array of numbers

int numbers[100];

//promt user to enter "How many numbers do you want to enter"

cout<<"How many numbers do you want to enter?: ";

//read total Number

cin>>totalNumber;

for(int i=0;i<totalNumber;i++){

cout<<"Enter value: ";

cin>>numbers[i];

}

//check if velues in array is prime or not

for(int i=0;i<totalNumber;i++){

if(IsPrime(numbers[i])){

cout<<numbers[i]<<" is prime.\n";

}else{

cout<<numbers[i]<<" is NOT prime.\n";

}

}

//delay

system("pause");

//exit program

return 0;

}

//Check is number is prime ot not

bool IsPrime(int inputnumber){

if ((inputnumber & 1) == 0)

{

//check if number =2

if (inputnumber == 2)

{

return true;

}

else

{

return false;

}

}

//Loop and find if number is prime or not

for (int i = 3; (i * i) <= inputnumber; i += 2)

{

if ((inputnumber % i) == 0)

{

return false;

}

}

return inputnumber != 1;

}

using namespace std;

bool IsPrime(int inputnumber);

//main method

int main()

{

//total number to input

int totalNumber=0;

//array of numbers

int numbers[100];

//promt user to enter "How many numbers do you want to enter"

cout<<"How many numbers do you want to enter?: ";

//read total Number

cin>>totalNumber;

for(int i=0;i<totalNumber;i++){

cout<<"Enter value: ";

cin>>numbers[i];

}

//check if velues in array is prime or not

for(int i=0;i<totalNumber;i++){

if(IsPrime(numbers[i])){

cout<<numbers[i]<<" is prime.\n";

}else{

cout<<numbers[i]<<" is NOT prime.\n";

}

}

//delay

system("pause");

//exit program

return 0;

}

//Check is number is prime ot not

bool IsPrime(int inputnumber){

if ((inputnumber & 1) == 0)

{

//check if number =2

if (inputnumber == 2)

{

return true;

}

else

{

return false;

}

}

//Loop and find if number is prime or not

for (int i = 3; (i * i) <= inputnumber; i += 2)

{

if ((inputnumber % i) == 0)

{

return false;

}

}

return inputnumber != 1;

}

#include <iostream>

using namespace std;

bool IsPrime(int inputnumber);

//main method

int main()

{

//total number to input

int totalNumber=0;

//array of numbers

int numbers[100];

//promt user to enter "How many numbers do you want to enter"

cout<<"How many numbers do you want to enter?: ";

//read total Number

cin>>totalNumber;

for(int i=0;i<totalNumber;i++){

cout<<"Enter value: ";

cin>>numbers[i];

}

//check if velues in array is prime or not

for(int i=0;i<totalNumber;i++){

if(IsPrime(numbers[i])){

cout<<numbers[i]<<" is prime.\n";

}else{

cout<<numbers[i]<<" is NOT prime.\n";

}

}

//delay

system("pause");

//exit program

return 0;

}

//Check is number is prime ot not

bool IsPrime(int inputnumber){

if ((inputnumber & 1) == 0)

{

//check if number =2

if (inputnumber == 2)

{

return true;

}

else

{

return false;

}

}

//Loop and find if number is prime or not

for (int i = 3; (i * i) <= inputnumber; i += 2)

{

if ((inputnumber % i) == 0)

{

return false;

}

}

return inputnumber != 1;

}

``

Learn more about our help with Assignments: C++

## Comments

## Leave a comment