But how do I ask user to choose a number and previous prime before that and after is displayed. And how many integers are in there depending on user input number?
#include <iostream>
using namespace std;
bool isPrimeNumber(int number)
{
if (number <= 1)
return false;
for (int i = 2; i < number; i++)
if (number % i == 0)
return false;
return true;
}
int main()
{
int prev, next;
cout<<"Enter a number\n";
int number;
cin>>number;
for(int i=number-1; i>=0; i--){
if(isPrimeNumber(i)==true){
prev=i;
break;
}
}
for(int i=number +1; i<number * 2; i++){
if(isPrimeNumber(i)==true){
next = i;
break;
}
}
cout<<"Prevous Prime: "<<prev<<endl;
cout<<"Next Prime: "<<next<<endl;
return 0;
}
Comments
Leave a comment