How can I make a boolean function that tells if a number is prime or not?

My specifications are to prompt the user for an integer, then display a message indicating if the number is prime.
I know that a boolean value can only return as true or false. I also know that 0 means that the condition is false. How can I change my int function into a boolean function that the main function calls on, then states whether the number is prime or not?

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

using namespace std;

int prime(int num);
int main(int argc, char *argv[])
{
    int value, prm;    
    cout << "Enter an integer: ";
    cin >> value;
    prm = prime (value);
    system("PAUSE");
    return EXIT_SUCCESS;
}
int prime(int num){
    for (int divisor = 1; divisor <= num; divisor++) {
        if (num%divisor==0){
           return false;
           }
           return true;
        }
}
Make function prime return bool, not int ;) Also change (in main) prm to bool. bools are an integral type (so int i = true; is valid), but it just makes more sense (imho) to make it return a bool.

Start your loop at 2 instead of 1 and end at sqrt(num) (ie see if numbers in the range 2 to sqrt(num) divide num).

The way you have it now, you check to see if divisor divides num, and if num does divide divisor, it returns flase, otherwise it returns true. Think about that for a second ;) You only want to return true after you have checked every number from 2 to sqrt(num) (meaning, after the loop has completed).

You also fail to inform the user of whether or not the number is prime. Is that by design or did you just omit it or something else?
If you want your function to return a boolean you have to change "int" prime to "????" prime. Also, if you want to print out true or false on your screen you'll have to change one of your variable's to bool(I know you'll figure out which one) and add something like this:

cout << boolalpha << variableyouareusing << endl;

Your if statement is not correct. It will always return 0/false, you have to work on it. One way to fix it is count how many times it was able to successfully get that % 0.

I hope I was able to help, but not too much. ;)
Last edited on
Now I have this, as I added suggestions from my teacher and from my main lessons, but my program is returning all sorts of error messages when I try to compile it and I'm really confused about how to make the boolean function return text in the main function.. Also I am unsure how to call the function in the main function correctly.

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
#include <cstdlib>
#include <iostream>

using namespace std;

bool isPrime(int prime);
int main(int argc, char *argv[])
{   
    int prime;
    cout << "Enter an integer: ";
    cin >> prime;
    bool isNumberPrime(prime);
    if (isNumberPrime(prime) {
       cout << prime << " is a prime number." << endl;
}
    else {
         cout << prime << " is not a prime number." << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
bool isNumberPrime(int prime){
    for (int divisor = 2; divisor <=a/2; divisor++) {
        if (prime%divisor==0)
        return 0;
           }
           return true;
        }
}
all sorts of error messages when I try to compile it

Copy + paste ;) It would help a bunches if you post the error messages.

1. You cannot define a function inside of another function,
2. The function prototype says the function is called isPrime, you make (what appears to be) another function prototype inside of main for a function isNumberPrime. Pick one name and leave the prototype before the main function,
3. By "a" do you mean "prime": for (int divisor = 2; divisor <=a/2; divisor++)? And I also said last time that you only need to check up to sqrt(prime),
4. You are missing a closing parenthesis for your if statement: if (isNumberPrime(prime) ) //<-- this one

I think that that is all that I had to fix to get it to run. Try it and see.
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
#include <cstdlib>
#include <iostream>

using namespace std;

bool isPrime(int prime);
int main(int argc, char *argv[])
{   
    int prime;
    cout << "Enter an integer: ";
    cin >> prime;
    bool isPrime(prime);
    if (isPrime(prime)) {
       cout << prime << " is a prime number." << endl;
}
    else {
         cout << prime << " is not a prime number." << endl;
         }
    system("PAUSE");
    return EXIT_SUCCESS;
}
bool isPrime(int prime){
    for (int divisor = 2; divisor <=prime/2; divisor++) {
        if (prime%divisor==0)
        return 0;
           }
           return true;
        }


Here is what I have now and the error messages I receive are
" C:\Dev-Cpp\prime.cpp In function `int main(int, char**)':
18 C:\Dev-Cpp\prime.cpp `isPrime' cannot be used as a function"
Remove line 12.
It works! Thank you so much for all your help and patience!
@Emma

Just a tip for the future: Your placement of the closing braces is a little confusing. For example on line 15, it looks as though it belongs to main, but it actually belongs to the if on line 13.

The answer is to put closing braces in the same column as the statement that needed an opening brace, like this :

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
31
32
#include <cstdlib> // is this needed?
#include <iostream>

using namespace std; // not such a good idea to do this, can cause naming conflicts

bool isPrime(int prime);

int main(int argc, char *argv[])
{   
    int prime;
    std::cout << "Enter an integer: ";
    std::cin >> prime;
   
    if (isPrime(prime)) {
       std::cout << prime << " is a prime number." << endl;
    }
    else {
         std::cout << prime << " is not a prime number." << endl;
    }
    std::system("PAUSE");
    return EXIT_SUCCESS;
} // end of main

bool isPrime(int prime) {
    for (int divisor = 2; divisor <=prime/2; divisor++) {
        if (prime%divisor==0) { // good practice to use braces even if there is 1 statement
             return false;  // use true or false for bool values (just my preference)
        }

    }
    return true;  // confusion - is this in the right place? should it be inside the for loop?
}


Emma Naylor wrote:
It works!


Does it?

With prime numbers, one only has to check divisors as far as sqrt(TheNumber).

Your isPrime function in it's current form always returns true, no matter what the number is. If reorganised to have the return true; as the else statement, then it returns true for the first prime number, not the one you wish to test for.

Hope all is well.
Last edited on
Your isPrime function in it's current form always returns true, no matter what the number is.

Huh? If I enter 12 I get false (100 is false as well, and 101 is true):

http://ideone.com/TxOyKu

12 is not a prime number.


return true; // confusion - is this in the right place? should it be inside the for loop?
We covered (and fixed) that already ;)
I wrote:
You only want to return true after you have checked every number from 2 to sqrt(num) (meaning, after the loop has completed).
Topic archived. No new replies allowed.