Hi lets start with code correctness in relationship to standard c++ and improve some parts too
#include <iostream>
using namespace std;
bool PrimeTest(int);
int main() //prefer int main() instead of void main() or void main(void)
{
int num;
cout<<"Enter number: ";
cin>>num;
if (PrimeTest(num))
cout<<num<<" is a prime number!";
else
cout<<num<<" is not a prime number!";
return 0;
}
bool PrimeTest(int n)
{
bool result=true;
for(short i=2;i<=(n-1);i++)
{
if (n%i==0)
{
result=false;
break;
}
else
{
result=true;
}
}
return result; //added because must return sth now
}
Keep in mind that important for u is to decrease the execution time of the PrimeTest function. When u included there the cout statements, u added extra overhead of msecs through these into the function. So if u place the call to your PrimeTest within a for loop and repeat the call for 1000 times u will notice a relatively big difference (if u r concerned for msecs time) when including those cout statements within the function. for example try:
for (int k=0;k<1000;++k)
{
if (PrimeTest(num))
cout<<num<<" is a prime number!";
else
cout<<num<<" is not a prime number!";
}
with the code i posted and with yours:
for (int k=0;k<1000;++k)
{
PrimeTest(num);
}
and compare