# Thread: Prime number program not working with odd non-prime numbers

1. ## Prime number program not working with odd non-prime numbers

I have a program that gets prime numbers that doesn't work. It looks like it works for all prime numbers except odd non-prime ones.
Here is the code:
Code:
```#include <iostream>
using namespace std;
bool isPrime(int prime) {
while(true) {
int track = 0;
track++;
if(prime >= track) {
if(prime % 2 == 0) {
return true;
}
else if(prime % 2 != 0) {
return false;
}

}

else {
return 1;
break;
}
}

}
int main ( ) {
int number;
cout << "Enter number: ";
cin >> number;
cout << "" << endl;

if(number == 1) {
cout << number << " is prime.";
return 0;
}
if(number == 2) {
cout << number << " is prime.";
return 0;
}

else if ( isPrime(number) ) {
cout << number << " is not prime.";
}
else {
cout << number << " is prime.";
return 0;
}

}```
Don't post code as a reply, only help.  3. Done.

Code:
```#include <iostream>
using namespace std;
bool isPrime(int prime) {
while(true) {
int track = 0;
track++;
if(prime >= track) {
if(prime % 2 == 0) {
return true;
}
else if(prime % 2 != 0) {
return false;
}

}

else {
return 1;
break;
}
}

}
int main ( ) {
int number;
cout << "Enter number: ";
cin >> number;
cout << "" << endl;

if(number == 1) {
cout << number << " is prime.";
return 0;
}
if(number == 2) {
cout << number << " is prime.";
return 0;
}

else if ( isPrime(number) ) {
cout << number << " is not prime.";
}
else {
cout << number << " is prime.";
return 0;
}

}``` 4. Unfortunately, that is poorly done. Rather, within the body of a function, or if statement, or loop, or other block of code, you should indent by one level. When that scope ends, you should reduce the indentation by one level. For example:
Code:
```#include <iostream>

using namespace std;

bool isPrime(int prime) {
while (true) {
int track = 0;
track++;
if (prime >= track) {
if (prime % 2 == 0) {
return true;
}
else if (prime % 2 != 0) {
return false;
}
}
else {
return 1;
break;
}
}
}

int main() {
int number;
cout << "Enter number: ";
cin >> number;
cout << "" << endl;

if (number == 1) {
cout << number << " is prime.";
return 0;
}
if (number == 2) {
cout << number << " is prime.";
return 0;
}
else if (isPrime(number)) {
cout << number << " is not prime.";
}
else {
cout << number << " is prime.";
return 0;
}
}```
Now, the first question I pose to you is this: under what conditions should isPrime return true, and under what conditions should isPrime return false? Note that you wrote:
Code:
```else if (isPrime(number)) {
cout << number << " is not prime.";
}
else {
cout << number << " is prime.";
return 0;
}```
In other words, it sounds like isPrime returns true if the number is not prime, but that would contradict the ordinary understanding of the name of the function.

Next, doing this in the body of the loop in isPrime:
Code:
```int track = 0;
track++;```
has the net effect of:
Code:
`int track = 1;`
You probably did not intend this.

Furthermore, this:
Code:
```if (prime % 2 == 0) {
return true;
}
else if (prime % 2 != 0) {
return false;
}```
is effectively the same as:
Code:
```if (prime % 2 == 0) {
return true;
} else {
return false;
}```
If you make this amendment to the properly indented version of your code, you will then see that your loop actually only runs for one iteration: every possible branch ends in a return statement.

Speaking of return statements: you have too many of them in your main function. Popular pages Recent additions 