always answer falso???

Hello! Just trying this one, always answer false!!!
Does someone see the mistake?
Please, help!!!
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 PrimeNumber (const int st) {

for (int i=2; i<st; i++){
if (st%i==0)
cout << i<<endl;
return false;

}
return true;


}
int main(){
int a=11;
bool b = PrimeNumber(a);
cout << b<<endl;

return 0;
}
Your mistake:-
1
2
3
4
5
6
for (int i=2; i<st; i++)
{
   if (st%i==0)
   cout << i<<endl;
   return false;
}


It should be:-
1
2
3
4
5
6
7
8
for (int i=2; i<st; i++)
{
   if (st%i==0)
   {
      cout << i<<endl;
      return false;
   }
}
1
2
3
if (st%i==0)
cout << i<<endl;
return false;

is not the same as:
1
2
3
4
5
if (st%i==0)
{
cout << i<<endl;
return false;
}

it is interpreted as:
1
2
3
4
5
6
if (st%i==0)
{
cout << i<<endl;
}

return false;


So even if st%i==0 is true, it will still return false;
Last edited on
Hello!
What the mistake is it now?

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



bool Prime (const int st) {

for (int i=2; i<st; i++){
if (st%i==0)
{
cout << i<<endl;
return false;

}
return true;

}


int main(){
int a=11;
bool b = Prime(a);
cout << b;
return 0;
}



Line 20: error: a function-definition is not allowed here before '{' token ????
You haven't closed all your braces. You added an extra open-brace around the code in your "if" block, but not a close-brace.
You're missing the closing brace in you Prime function.
Topic archived. No new replies allowed.