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);
}
When creating functions or using them always remember to start with the signature.
Think what will this function need to work with, and what will it return. You need to return if something is true or false, which are values of data type bool. Just like in a conditional such an if statement you may use Boolean operators like ==, != and etc.
So you need to return a bool and check if two numbers are divisible. Therefore:
bool Divisible(int a, int b){
// == boolean operator that will return true if a%b evaluates to 0
// false if not
return (a % b) == 0
}
Once you start thinking of functions this way, every program becomes one great puzzle!