Write a C++ program that checks if a given integer is even or odd.
Input and Output Examples
- Input: 4
Output: 4 is even. - Input: 7
Output: 7 is odd.
Algorithm:
- Start the Program: Include necessary headers and declare the use of the standard namespace.
- Prompt for Input: Request the user to enter an integer.
- Read the Input: Store the integer in a variable.
- Check Even/Odd: Determine if the number is even or odd using the modulus operator.
- Display the Output: Print whether the number is even or odd.
- End the Program: Finish execution cleanly.
Below is a C++ program that uses the modulus operator to check if a number is even or odd:
cpp
#include <iostream>
using namespace std;
int main() {
int num; // Declare an integer to store the user input.
// Take input values
cout << "Enter an integer: ";
cin >> num;
// Check if the number is even or odd using the modulus operator
if (num % 2 == 0) {
cout << num << " is even." << endl; // Output if the number is even.
} else {
cout << num << " is odd." << endl; // Output if the number is odd.
}
return 0; // End the program successfully.
}