Lexington High School Mathematics Math Dept. Front Page  | Student Links  | Family Links  | Q&A
Introduction to C++, Mrs. Haupt Term 1 Assignment List | Term 2 Assignment List | Intro. to C++ Info

Sample Code for Assignment 17

// Assignment 17  -  Divisible Function using bool

#include <iostream.h>

bool divisible(int num1, int num2); // function to check divisibility of numbers

int main()
{
   int num1, num2;                  // user input numbers
   bool no_remainder;               // boolean used for checking divisibility

   cout << endl << "Please enter your two numbers : ";
   cin >> num1 >> num2;

   no_remainder = divisible(num1, num2);

   if (no_remainder)
      cout << num1 << " is divisible by " << num2 << "." << endl << endl;
   else
      cout << num1 << " is NOT divisible by " << num2 << "." << endl << endl;

   return 0;
}

bool divisible(int num1, int num2) // function to check divisibility of numbers
{
   if ((num1 % num2) == 0)
      return true;             // modulo is zero, no remainder, is divisible
   else
      return false;            // not divisible
}