How to correctly use Boolean functions?

c++

Solution

The question asks you to write a method that takes the numbers as parameters, not let's you input them from standard input.

Boolean is a type of its own in c++, so you want the method to return bool and not int. An easy to read solution:

bool Divisible(int a, int b) {
    int remainder = a % b; // Calculate the remainder of a and b.

    if(remainder == 0) {
        return true; //If the remainder is 0, the numbers are divisible.
    } else {
        return false; // Otherwise, they aren't.
    }
}

Or more concise:

bool Divisible(int a, int b) {
    return (a % b) == 0;
}

Even more concise:

bool Divisible(int a, int b) {
    return !(a % b);
}

Problem

I'm having trouble with the following assignment, mostly because I don't understand how a Boolean function works. "Write a function called Divisible that takes in two numbers as parameters. Return True if the first number is evenly divisible (no remainder) by the second number. Otherwise return False. Hint: Use %" Currently what I have is: ``` int Divisible() { int firstNum; int secondNum; int result; cout << "Please enter any integer: "; cin >> firstNum; cout << "Please enter another integer: "; cin >> secondNum; result == firstNum%secondNum; } ``` I'm not sure what to do beyond that. I thought I could assign bool = 0 as true but that doesn't appear to be the case. I'm still very new to C++ so any help would be appreciated.

Original source

Related problems