Question: An integer is called a prime number if a natural number that has exactly two distinct natural number divisors: 1 and itself. For example, 2,
An integer is called a prime number if a natural number that has exactly two distinct natural number divisors: 1 and itself. For example, 2, 3, 5, 7, 11, 13 are prime numbers. Write a C++ recursive function called isPrime to determine whether an integer is a prime number. The recursive function isPrime has two parameters: the first one is the test number p and the second one is a divisor q which is greater or equal to 2 and less than p. The function will return 1 if p=q (it is better if (q*q>p), why?) and return 0 if p%q = 0 (note that, in this case, remainder is 0 and p>q). Those two cases are base cases. Then recursively call isPrime(p, q+1). In your main function, prompt user to input a positive number then call the recursive function isPrime(p,2). If the function returns 1, output The number XX (the input number) is a prime number, otherwise, The number XX is not a prime number
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
