c Implement a function that takes an integer value and retur

c++

Implement a function that takes an integer value and returns true if the integer is a prime number and false otherwise.

The function should have the following signature.

bool isPrime(int n)

Include the function in a console program that calls the isPrime function with a range of values that test several common cases as well as the boundary cases. Use an assert statement for each test case.

Solution

Primes.cpp

#include <iostream>
#include <cassert>
using namespace std;
bool isPrime(int n);
int main()
{
assert(isPrime(5) == true);
assert(isPrime(7) == true);
assert(isPrime(17) == true);
assert(isPrime(61) == true);
assert(isPrime(15) == false);
assert(isPrime(25) == false);
assert(isPrime(55) == false);

return 0;
}
bool isPrime(int n){
for (int f = 2; f <= n / 2; f++) {
   if (n % f == 0) {
   return false;
   }
  
   }
   return true;
}

c++ Implement a function that takes an integer value and returns true if the integer is a prime number and false otherwise. The function should have the followi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site