Program to find a prime number next to given integer
// CPP program to find a prime number next to a given integer
#include"iostream"
using namespace std;
int func(int n)
{
int count=0;
for(int i=2; i<=n; i++)
{
if(n % i == 0)
{
count++;
}
}
if(count < 2)
{
return n;
}
else
{
func(n+1);
}
}
int main()
{
int num;
cout<<"Enter a number: ";
cin>>num;
int res = func(num);
cout<<"The prime number next to "<<num<<" is: ";
cout<<res<<endl;
}
Output:
#include"iostream"
using namespace std;
int func(int n)
{
int count=0;
for(int i=2; i<=n; i++)
{
if(n % i == 0)
{
count++;
}
}
if(count < 2)
{
return n;
}
else
{
func(n+1);
}
}
int main()
{
int num;
cout<<"Enter a number: ";
cin>>num;
int res = func(num);
cout<<"The prime number next to "<<num<<" is: ";
cout<<res<<endl;
}
Output:
Comments
Post a Comment