Determine if a number is prime

Hi, I am taking a class in computer programming (it just started so I don't know much).

I need to write a function bool function that determines if a number entered by the user is prime, and a driver program for it. My problem now is that it keeps saying all numbers i enter are prime. If you have any suggestions that might be helpful I would appreciate them.

here is what i have so far:


// A function deciding if a number is prime

#include <iostream>
using namespace std;

//Determine if a positive interger is prime
bool isprime (int number)
{
int count=2;
int num;
num = number;

do
{
count = count +1;
}while ((num % count) > 0);
return true;

if ((num % count) >= 0)
return false;

}


int main()
{
int n;

cout << "Enter a number to find out if it is prime: ";
cin >> n;

if (isprime (n) == true)
cout << "The number is Prime" << endl;

else if (isprime (n) == false)
cout << "The number is not prime" << endl;

return 0;
}


P.S. sorry if this problem was already posted. I couldn't find it if it was.
Last edited on
You are struggling with flow control.

Your first do..while loop has no effect on the program's output, because:

The next statement is an unconditional return. Hence, the function returns true no matter what.

The following if statement is never executed (since you already returned).


Think about how the loop and if statements relate to solve your flow control problem.

Hope this helps.
Last edited on
oh... that makes sense. thanks
Topic archived. No new replies allowed.