Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a recursive function to determine if a number is prime. Skeletal code is provided in the IsPrime function. #include using namespace std; // Returns

Implement a recursive function to determine if a number is prime. Skeletal code is provided in the IsPrime function.

#include

using namespace std;

// Returns 0 if value is not prime, 1 if value is prime

int IsPrime(int testVal, int divVal)

{

// Base case 1: 0 and 1 are not prime, testVal is not prime

// Base case 2: testVal only divisible by 1, testVal is prime

// Recursive Case

// Check if testVal can be evenly divided by divVal

// Hint: use the % operator

// If not, recursive call to isPrime with testVal and (divVal - 1)

return 0;

}

int main(){

int primeCheckVal = 0; // Value checked for prime

// Check primes for values 1 to 10

for (primeCheckVal = 1; primeCheckVal <= 10; ++primeCheckVal) {

if (IsPrime(primeCheckVal, (primeCheckVal - 1)) == 1) {

cout << primeCheckVal << " is prime." << endl;

}

else {

cout << primeCheckVal << " is not prime." << endl;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions

Question

Find each sum. 2 + 4

Answered: 1 week ago

Question

4. Support and enliven your speech with effective research

Answered: 1 week ago

Question

3. Choose an appropriate topic and develop it

Answered: 1 week ago