whats wrong with my bool script?

Im trying to use this function to find whether a user input integer is a prime number or not:
here is my code with a bool function:
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
26
27
28
29
30
#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int);

int main(){
        int i,x,z;
       cout<<"Input a positive integer"<<endl;
    cin>>x;
    while (x<=0){
        cout<<"Input a positive integer"<<endl;
        cin>>x;}
if (isPrime(x)){
    cout<<isPrime(x)<<"is a prime number."<<endl;
}
else {
    cout<<isPrime(x)<<" is not a prime number."<<endl;
}

    }
bool isPrime(int){
    int x,i;
    for (i=2; i<sqrt(x); i++){
            if (x%i==0){
                return false;}
    else
        return true;
        }}



It just seems like no matter what integer i input (even 5 and 13) i will end up with the boolean function returning me a false or (0) output.

May i know what is wrong here with my codes?
 
for (int i = 2; i < sqrt(number+1); i++)


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool isPrime(const int & number)
{
    if (number < 2)
    {
            return false;
    }
    
    for (int i = 2; i < (number / 2 + 1); i++)
    {
        if (number%i == 0)
        {
            return false;
        }
    }
    return true;
}
Last edited on
For a start, inside your isPrime function, you're using x without ever initialising it.

I'm surprised your compiler didn't warn you about that.
should look like this

1
2
3
4
5
6
7
8
bool isPrime(int x){
    int i;
    for (i=2; i<sqrt(x); i++){
            if (x%i==0){
                return false;}
    else
        return true;
        }}


x is undefined at the point you are using it: x%i, x could be anything here
thanks for the advice,however i noticed something wierd,

when input value for x = 2469,
the script returns (true) and indicates that 2469 is a prime number.
but 2469 in fact is not a prime,it is divisible by 3 with no remainder..
does anyone know why does this happen?
If you've fixed the problem that mik2718 and I told you about, it should work fine.

Without seeing your code, we can't possibly know why your new version is not working.
yeap, ive done the ammendments as reccomended by mik2718,Thanks alot Mik!! your help is really appreciated!

below is my edited code which is having problem determining whether input value such as 2469 is prime. (while smaller prime numbers like 7,11,13 are working fine..)



#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int);

int main(){
int i,x,z;
cout<<"Input a positive integer"<<endl;
cin>>x;
while (x<=0){
cout<<"Input a positive integer"<<endl;
cin>>x;}
if (isPrime(x)){
cout<<x<<" is a prime number."<<endl;
}
else {
cout<<x<<" is not a prime number."<<endl;
}

}
bool isPrime(int x){
int i;
for (i=2; i<floor(sqrt(x)); i++){
if (x%i==0){
return false;}
else
return true;
}}
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) Look at the logic here:

1
2
3
4
5
6
for (i=2; i<floor(sqrt(x)); i++){
   if (x%i==0){
      return false;}
   else
      return true;
}


On the very first iteration, i.e. when i is 2, this will return true if x%i==0 is false. The loop will never proceed to testing any more values of i.
Last edited on
then in that case i should get the right answer that states 2469 is not a prime number,



but in this case however im getting the outcome which states that 2469 is a prime number...
then in that case i should get the right answer that states 2469 is not a prime number,

No. The first test is for i = 2. 2469 is not divisible by 2, so (x%i==0) is false, and your function returns true.
Thanks for your explanation. I understood it.
Topic archived. No new replies allowed.