Jon wrote:
> Like nobody has seen this program before...ha
> 
> I have built a program that takes in an integer and lets the user 
> know if it is prime or not.  I wrote a bool function to handle to the 
> calculation but could not get it to return anything but "true".  When 
> I changed to bool to a regular int isPrime program and passed an 
> actual value, 0 or 1, is seemed to work.  Is there something I am 
> missing when I am returning a Boolean value?  I placed both program 
> below to see if I can get any input or pointers.  I didn't have to 
> use bool for the class, but I prefer to.

What compiler?


> bool isPrime(int number)
> {     
> 
>       for( int i=2; i < number; i++)
>       {
>               int leftOver=(number % i);
>               
>               if (leftOver==0)
>               {
>                       return true;
>                       break;
>               }
> 
>       }               
>               return false;
> }

A few issues:

1)  2 is prime.  Your code doesn't account for that.
2)  Also, you only have to look at the first (int)sqrt(number) numbers.
3)  The break; statement is not needed.  Your compiler warnings should 
be turned up high enough so that you get notified of such things.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to