Code:
#include <iostream>
using namespace std;
// Prime number function - Tests number for remainder. If none is found, return false. Otherwise, return true.
bool PrimeFunction()
{
int n;
cin >> n;
cin.ignore();
// You need to check if n > 0, right? Then we don't need a loop, but a condition.
if (n > 0)
{
if (n % 2 == 0)
{
cout << "Number is not prime";
return false;
}
else
{
cout << "Number is prime";
return true;
}
}
// If n == 0, the condition won't execute, right? So we need to make we return something here too! 0 is a prime, I think?
return true;
}
int main()
{
cout << "Enter a number and press enter: \n";
PrimeFunction();
cin.get();
return 0;
}
If you want a little more advanced (or how I would do it):
Code:
#include <iostream>
using namespace std;
// Prime number function - Tests number for remainder. If none is found, return false. Otherwise, return true.
bool PrimeFunction()
{
int n;
cin >> n;
cin.ignore();
// If n == 0 OR the number is even dividable by 2, then it's a prime. Otherwise it's not.
if (n == 0 || n % 2 == 0)
{
cout << "Number is not prime";
return false;
}
else
{
cout << "Number is prime";
return true;
}
}
int main()
{
cout << "Enter a number and press enter: \n";
PrimeFunction();
cin.get();
return 0;
}