#include <iostream>
usingnamespace std;
int main ()
{
int n;
cout << "Enter an intger !"<<endl;
cin>>n;
if(n == 2)
cout<<"2 is the only even prime number"<<endl;
elseif (n%2==0)
cout << n<< " is not prime number" << endl;
if ((n%3==0) || (n%5==0) || (n%7==0))
cout<< n<<" is prime number"<<endl;
elseif (n%2==1)
cout<<n<<" is mot a prime number"<< endl;
system("pause");
return 0;
}
Unless the user input is limited to a very small range of values, using only if-else is not really feasible. The amount of coding required could be huge (unless you used some sort of macro to construct the code).
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
bool IsPrime(unsigned& test_number, unsigned check_number) {
++check_number;
if (test_number == check_number)
returntrue;
if (test_number % check_number == 0) {
cout << test_number << " can be divided by " << check_number << endl;
returnfalse;
}
return IsPrime(test_number, check_number);
}
int main() {
cout << "Enter a number > 0: ";
unsigned test_number = 0;
cin >> test_number;
if (IsPrime(test_number, 1))
cout << test_number << " is a prime number" << endl;
else
cout << test_number << " is not a prime number" << endl;
return 0;
}
I actually think you can do it using template recursion too, the number of values you'd accept would be limited by how much recursion you did and compile time would increase as well, but it should be possible.