Help with Functions

Can someone help me understand whats the use of number in this function

bool isDiv (int number, int div)
{
return number%div;
}
bool isPrime (int number)
{
for ( int i = 2; i < number; i++)

{
if ( isDivisible( number, i ) )
{
return false;
}
}
return true;
}
I would guess that number is a value that was entered by the user in a main function and that is being sent to the isPrime function. If this is supposed to be determining whether number is prime, I think the logic is a little off.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool isDiv (int number, int div)
{
    return number%div;
}

bool isPrime (int number)
{
    for ( int i = 2; i < number; i++)
    {
        if ( isDivisible( number, i ) ) // should be isDiv ?
        {
            return false;
        }
    }
    return true;
}
As wildblue said, your logic is off. number%div calculates the remainder. If number is indeed a divisor, the remainder should be 0. However, if there is a remainder, you're returning true, which is the opposite of what you want.

SHould be as follows:
1
2
3
bool isDiv (int number, int div)
{  return (number%div) == 0;  //  Return true if NO remainder
}


Topic archived. No new replies allowed.