Task should be accomplished by a user defined function(s).A prime number is an integer that has no factors other than one and itself. For example, 2, 3, 5, and 7 are prime while 9, 10, and 15 are not.Write a function IsPrime() that accepts a long greater than 1 as a parameter and returns a bool. The function should return true if and only if the integer is prime.
#include <iostream>
using namespace std;
bool isPrime(int n)
{
static int i=2;
if (n == 0 || n == 1) {
return false;
}
// Checking Prime
if (n == i)
return true;
// base cases
if (n % i == 0) {
return false;
}
i++;
return isPrime(n);
}
// Driver Code
int main()
{
isPrime(15) ? cout << " true\n" : cout << " false\n";
return 0;
}
Comments
Leave a comment