/*********************************** * Modify this program so that it * determines whether an integer * entered by the user is positive, * zero or negative and prints the * result. ***********************************/ #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 classifies the integer. std::cout << n << " is positive." << std::endl; return 0; } // Hint: // // There are three possibilities, so use a cascading // conditional statement with two conditions followed // by a final alternative: // // if ( ... ) { // ... // } else if ( ... ) { // ... // } else { // ... // } // Here is one solution: /* int main() { int n{}; std::cout << "Enter an integer: "; std::cin >> n; if ( n > 0 ) { std::cout << n << " is positive." << std::endl; } else if ( n == 0 ) { std::cout << n << " is zero." << std::endl; } else { std::cout << n << " is negative." << std::endl; } return 0; } */ // Do not unnecessarily test a third condition: // // if ( n > 0 ) { // std::cout << n << " is positive." << std::endl; // } else if ( n == 0 ) { // std::cout << n << " is zero." << std::endl; // } else if ( n < 0 ) { // std::cout << n << " is negative." << std::endl; // } // // If the first two conditions are false, then we already // know that // // n <= 0 and n != 0, // // and therefore 'n' must be negative. There is no need // to test another condition. Simply use an 'else' block.