Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / How to correctly use Boolean functions?

How to correctly use Boolean functions?

August 20, 2021 by James Palmer

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!

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in