/*********************************** * Modify this program so that it * determines whether an integer * entered by the user is even or * odd and prints the result. * * Be careful about negative * integers. ***********************************/ #include // Function declarations int main(); // Function definitions int main() { int n{}; std::cout << "Enter an integer: "; std::cin >> n; // Modify the following statement so that the // program correctly identifies both even and // odd integers. std::cout << n << " is even." << std::endl; return 0; } // Hint: // // You will need to use the remainder operator '%'. // // What are the values of n%2 for different values of 'n'? // // In particular, try both positive and negative integers. // You may have noticed the following: // // If 'n' is even, then // // n%2 == 0 // // regardless of whether 'n' is positive, negative or zero. // // If 'n' is odd, then // // n%2 == 1 if n > 0 // // and // // n%2 == -1 if n < 0. // // Consequently, it is simpler to test whether 'n' is even // than to test whether it has a remainder of 1. // Here is one solution: /* int main() { int n{}; std::cout << "Enter an integer: "; std::cin >> n; if ( n%2 == 0 ) { std::cout << n << " is even." << std::endl; } else { std::cout << n << " is odd." << std::endl; } return 0; } */