even and odd in c++
To determine whether a number is even or odd in C++, you can use the modulus operator (%). The modulus operator returns the remainder of a division operation.
Here are the steps to determine whether a number is even or odd in C++:
Declare a variable to store the input number, for example, "num".
Read the input number from the user and assign it to the "num" variable.
Use the modulus operator (%) to check if the remainder when "num" is divided by 2 is equal to 0.
If the remainder is 0, then the number is even. You can display a message to indicate that the number is even.
If the remainder is not 0, then the number is odd. You can display a message to indicate that the number is odd.
Here is a code example:
#include <iostream>
int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
if (num % 2 == 0) {
std::cout << "The number is even." << std::endl;
} else {
std::cout << "The number is odd." << std::endl;
}
return 0;
}
Note: This code snippet reads an integer from the user and checks if it is even or odd using the modulus operator. It then displays the corresponding message.