isPrime Function returning 9, 15, 21, 27...

I run this program and I get all the prime numbers I need and them some. It seems to give me non-prime numbers divisible by 3 starting at 9. Any help would be much appreciated!

#include <iostream>
#include <iomanip>

using namespace std;

bool isPrime(int);

int main()
{
cout << setw(25) << " " << "Prime Numbers" << endl;

for(int i = 2; i <= 500; i++){
if(isPrime(i) == true){
cout << i << " ";
}
}
return 0;
}

bool isPrime(int num){
for(int j = 2; j < num; j++){
if(num % j == 0){
return false;
}

return true;
}
}
1
2
3
4
5
6
7
8
9
bool isPrime(int num) {
	for(int j = 2; j < num; j++) {
		if(num % j == 0) {
			return false;
		}

		return true; //this is inside the loop
	}
}
THANK YOU!!! Man I feel dumb.
Topic archived. No new replies allowed.