First off, last time i checked a prime number was defined like this:
A prime number is a positive integer that has exactly two positive integer factors, 1 and itself. For example, if we list the factors of 28, we have 1, 2, 4, 7, 14, and 28. That's six factors. If we list the factors of 29, we only have 1 and 29. That's 2. So we say that 29 is a prime number, but 28 isn't.Another way of saying this is that a prime number is a whole number that is not the product of two smaller numbers. Note that the definition of a prime number doesn't allow 1 to be a prime number: 1 only has one factor, namely 1. Prime numbers have exactly two factors, not "at most two" or anything like that. When a number has more than two factors it is called a composite number.
#include <iostream.h>
int main()
{
int num;
cout << "\nPlease enter a number to check: ";
cin >> num;
if ((num%2) == 0)
{
cout << "\nNumber is not prime.\n";
}
else
{
cout << "\nNumber is prime.\n";
}
return 0;
}
This is how i'd do it. works for every prime except 2. if you want a list of prime numbers to test with there is a great list
here. It's the first 1000 prime numbers. I think you were getting mixed up witht eh square root part, you really don't need it unless your trying to output a whole bunch of prime numbers....not checking for just one. I could be wrong though. Can somebody back me up on this?