Write a c function with comment showing each step that is a

Write a c++ function with comment showing each step that is a recursive version of problem 2:

Problem 2:Write a c++ function that takes as input an integer. The function uses a while loop to

determine whether that number is a prime number or not, and returns True if it is, and False otherwise.

Solution

#include <iostream>

using namespace std;
bool isPrime(int n) {
int f = 2;
while ( f < n / 2) {
if (n % f == 0) {
return false;
}
f++;
}
return true;
}

int main()
{
int n;
cout<<\"Enter an integer: \";
cin >> n;
if(isPrime(n)){
cout<<\"Prime\"<<endl;
}
else{
cout<<\"Not Prime\"<<endl;
}
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter an integer: 37                                                                                                                                                                                                                                                   

Prime                                                                                                                                                                                                                                                                  

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter an integer: 33                                                                                                                                                                                                                                                   

Not Prime

Recursive Version

#include <iostream>

using namespace std;
bool isPrime(int n, int i){
if (i == 2)//if i value reaches 2 then it is a prime number
return true;
else{
if (n % i == 0){//checking divisible or not
return false;
}
else
return isPrime(n,i-1);//if not divisible then calling again for next check
}
}

int main()
{
int n;
cout<<\"Enter an integer: \";
cin >> n;
if(isPrime(n,n/2)){
cout<<\"Prime\"<<endl;
}
else{
cout<<\"Not Prime\"<<endl;
}
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter an integer: 33                                                                                                                                                                                                                                                   

Not Prime                                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter an integer: 37                                                                                                                                                                                                                                                   

Prime

Write a c++ function with comment showing each step that is a recursive version of problem 2: Problem 2:Write a c++ function that takes as input an integer. The
Write a c++ function with comment showing each step that is a recursive version of problem 2: Problem 2:Write a c++ function that takes as input an integer. The

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site