#include /**************************************************************** * Query the user for two integers, and write two loops: * * - The first starts at the smaller integer and proceeds * toward the larger integer with a step size of 2. For * example, given 2 and 7, it should print * 2 4 6 * * - The second prints only the even integers between the * smaller and larger integers, including either endpoint * if it is even. ****************************************************************/ // Function declarations int main(); // Function definitions int main() { int minimum{}; std::cout << "Enter a first integer: "; std::cin >> minimum; int maximum{}; std::cout << "Enter a second integer: "; std::cin >> maximum; if ( minimum > maximum ) { // Swap the two values. int tmp{ minimum }; minimum = maximum; maximum = tmp; } for ( ; ; ) { std::cout << 0 << " "; } std::cout << std::endl; for ( ; ; ) { std::cout << 0 << " "; } std::cout << std::endl; return 0; } // Hint: For the first loop, consider how you // can change the increment so that the loop // variable increases by 2 instead of 1. // // For the second loop, recall that an integer // is even if its remainder after division by // 2 is zero. /* Both of these approaches work: int main() { int minimum{}; std::cout << "Enter a first integer: "; std::cin >> minimum; int maximum{}; std::cout << "Enter a second integer: "; std::cin >> maximum; if ( minimum > maximum ) { // Swap the two values. int tmp{ minimum }; minimum = maximum; maximum = tmp; } // Start at the minimum and increase by 2. for ( int k{ minimum }; k <= maximum; k += 2 ) { std::cout << k << " "; } std::cout << std::endl; // Examine every integer, but print only // those that are even. for ( int k{ minimum }; k <= maximum; ++k ) { if ( k%2 == 0 ) { std::cout << k << " "; } } std::cout << std::endl; return 0; } // The second loop above can instead be written // by first finding the smallest even integer // in the interval and then increasing by 2. int main() { int minimum{}; std::cout << "Enter a first integer: "; std::cin >> minimum; int maximum{}; std::cout << "Enter a second integer: "; std::cin >> maximum; if ( minimum > maximum ) { // Swap the two values. int tmp{ minimum }; minimum = maximum; maximum = tmp; } // Ensure that 'minimum' is even. if ( minimum%2 != 0 ) { ++minimum; } for ( int k{ minimum }; k <= maximum; k += 2 ) { std::cout << k << " "; } std::cout << std::endl; return 0; } */