68 249
Assignments Done
99%
Successfully Done
In November 2018

Answer to Question #47753 in C++ for arundhati

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


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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions