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

isPrime function: A prime number is a number that is only evenly divisible by it

ID: 3761127 • Letter: I

Question

isPrime function:

A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6.

Write a function name isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the function in a complete program.

TIP: Recall that then % operator divides one number by another, and returns the remainder of the division.

Recursion MUST be used.

Explanation / Answer

bool isPrime(int n,int i)

{

if (i == 1)

{

return 1;

}

else

{

if (n%i == 0)

{

return 0;

}

else

{

return isPrime(n, i-1);

}

}

}