Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the given source code below. When finished, the program should prompt t

ID: 3541975 • Letter: C

Question

Complete the given source code below. When finished, the program should prompt the user for a integer value and then report whether or not the given number is prime.
Only the query to test for prime numbers needs to be completed, write the query definition below the main without changing its prototype.

NOTE: There are a few ways to construct this algorithm. Think it through before you start putting code to line and do not use answers from the internet.
As I stated before, a lot of stupid people use C++ and their code is terrible to understand/read and run. Do not follow in these people's footsteps.

#include <iostream>
#include <cstdlib>

using namespace std;

/* Returns whether or not the given integer is a prime number.
*
* Require: number > 0
* Ensure: if number is prime,
* returns true
* otherwise,
* returns false
*/
bool isPrime(int number);

int main() {
  int number = 0;
  cout << "Please enter an integer: ";
  cin >> number;
  cout << endl;

  if (isPrime(number)) {
    cout << "Your number is prime!" << endl;
  }
  else {
    cout << "Sorry, your number is composite." << endl;
  }

  exit(EXIT_SUCCESS);
}

Explanation / Answer

Code


#include <iostream>

#include <cstdlib>

using namespace std;


bool isPrime(int number);

int main() {

int number = 0;

cout << "Please enter an integer: ";

cin >> number;

cout << endl;

if (isPrime(number)) {

cout << "Your number is prime!" << endl;

}

else {

cout << "Sorry, your number is composite." << endl;

}

exit(EXIT_SUCCESS);

}

bool isPrime(int number){

bool flag = true;

for(int i=2;i<=number/2;i++){

if(number%i==0){

flag = false;

}

}

return flag;

}



  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote