A number is considered even if it is divisible by 2 without leaving a remainder. Conversely, a number is odd if it is not divisible by 2 without leaving a remainder.
C++ Code:
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0) {
cout << number << " is an even number." << endl;
} else {
cout << number << " is an odd number." << endl;
}
return 0;
}
Code explanation:
- We prompt the user to enter a number.
- We read the input number from the user.
- We check if the number is divisible by 2 using the modulo operator `%`.
- If the remainder is 0, we output that the number is even, otherwise, we output that the number is odd.
- Finally, the program returns 0 to indicate successful execution.