you could use a simple for loop in your main function and do a brute force method like your functions do or you could use the Sieve of Eratosthenes method which works very well. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
also on line 13 in your code you are using or which is incorrect in c++. || = or and && = and in c++
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
When testing for primes, you only need to test half the range:
bool is_prime = true;
cout << "Enter a numeral: ";
cin >> number;
if(number == 1 || number == 3)
{
cout << "Number is prime.";
exit(0);
}
for(int i=2; i<number/2; i++)
{
//If there is any case of no remainder, then number is not prime
if(number%i)
{
is_prime = false;
break;
}
}
if(is_prime)
cout << number << " is a prime number. ";
else
cout << number << " is not a prime number. ";
There might be easier ways to do this.. this is just something I came up with off the top of me' head.
Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
i bow before your compiler then ;). seriously i would use the standard operators since i doubt it would work on older compilers.
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189