Use the modulus operator to determine whether an integer is even. - C++ Operator

C++ examples for Operator:Arithmetic Operator

Description

Use the modulus operator to determine whether an integer is even.

Demo Code

#include <iostream>

bool isEven(int);

int main(int argc, const char *argv[]) {
    int input = 0;

    while (input >= 0) {
        std::cout << "Enter an integer (-1 to quit): ";
        std::cin >> input;//  w  ww.j  a va 2  s  .c  o  m

        if (input >= 0)
            std::cout << input << ":" << (isEven(input) ? "" : " not") << " even" << std::endl;
    }
    return 0;
}
// determine whether an integer is even
bool isEven(int x) { 
   return (x % 2 == 0); 
}

Result


Related Tutorials