In: Computer Science
Rewrite the function so the formal parameter x is an IO pointer-to int parameter. The function’s prototype is changed as shown below. Please explain code with comments
//--------------------------------------------------
void NextPrime(int *x)
//--------------------------------------------------
{
bool IsPrime(const int x);
}
//C++ program
#include<iostream>
#include<math.h>
using namespace std;
bool IsPrime(const int x){
//function to check whether a number given in argument
is prime or not
//if any number >=2 and less than equal to its
square root not divides x then x is prime
for(int i=2;i<=sqrt(x);i++){
if(x%i == 0)return false;
}
return true;
}
void NextPrime(int *x){
//increasing value of x by 1 to check whether next
element is prime or not
*x = *x+1;
//we keep increasing value x by 1 until we find x is a
prime
while(IsPrime(*x) == false){
*x = *x + 1;
}
}
//Driver main function
int main(){
int x = 13;
NextPrime(&x);
cout<<"Next Prime Number : "<<x;
return 0;
}
//sample output