Answer to Question #174527 in C++ for Luna

Question #174527

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.



1
Expert's answer
2021-03-22T23:59:06-0400
#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;

}

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

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS