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);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.