Header Ads Widget

Write a CPP function to find next prime number of a given number

 Code:-

#include<bits/stdc++.h>
using namespace std;

// function for checking prime number
int prime(int n)
{
int flag=0,i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==1)
return 1;
else
return 0;
}

// Driver function

int main()
{
int a,f=1,b;
cout<<"Enter a number : ";
cin>>a;
b=a; // Copy a into b for future use
while(f)
{
a++;
f=prime(a);
}
cout<<"Next prime number of "<<b<<" is : "<<a;
return 0;
}

Output:-

Enter a number : 15

Next prime number of 15 is : 17




Post a Comment

0 Comments