Finding Prime numbers with boolean function

I am trying to find the prime number from the function isPrime. If the number is prime it will give me true, if not it will give me false.

When I input 5 on the command prompt it gives me true, but when I input isPrime(5) I get false. How can I make it give me true for both inputs, 5 and isPrime(5)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

bool isPrime(int n){
    for(int i = 2; i <= n / 2; ++i){
      if(n % i == 0){
          return false;
      }
  }
}

int main(){
    int x;
	int n;
	cin >> n;
	x = isPrime(n);
	if(x == 0){
		cout << "false" << endl;
	}
	else{
		cout << "true" << endl;
	}

}
In isPrime(...) you never return true. So it is actually undefined what is returned when line 7 is not involved. So add return true; after line 9.
it is still giving me the same problem, i am getting false when i input 25 but true when i input isPrime(25)

edit
nvm i found my problem it works now thanks
Last edited on
Topic archived. No new replies allowed.