C++ Program to Check Even or Odd Number
In C++, you can check if a number is even or odd by using the modulus operator
% like this: if (num % 2 == 0) { /* even */ } else { /* odd */ }.Examples
Input4
Output4 is even
Input7
Output7 is odd
Input0
Output0 is even
How to Think About It
To check if a number is even or odd, think about dividing it by 2. If the remainder is zero, the number is even because it divides evenly. If the remainder is not zero, the number is odd because it leaves a leftover.
Algorithm
1
Get the input number from the user2
Calculate the remainder when the number is divided by 23
If the remainder is 0, the number is even4
Otherwise, the number is odd5
Print the resultCode
cpp
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; if (num % 2 == 0) { cout << num << " is even" << endl; } else { cout << num << " is odd" << endl; } return 0; }
Output
Enter a number: 7
7 is odd
Dry Run
Let's trace the input 7 through the code
1
Input number
User enters 7, so num = 7
2
Calculate remainder
7 % 2 equals 1 (since 7 divided by 2 leaves remainder 1)
3
Check remainder
Since remainder is 1 (not 0), number is odd
4
Print result
Output: "7 is odd"
| num | num % 2 | Condition | Output |
|---|---|---|---|
| 7 | 1 | 1 != 0 (odd) | 7 is odd |
Why This Works
Step 1: Using modulus operator
The % operator gives the remainder of division, which helps us find if a number divides evenly by 2.
Step 2: Even number check
If the remainder is 0, the number is even because it divides exactly by 2.
Step 3: Odd number check
If the remainder is not 0, the number is odd because it leaves a remainder when divided by 2.
Alternative Approaches
Using bitwise AND operator
cpp
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; if ((num & 1) == 0) { cout << num << " is even" << endl; } else { cout << num << " is odd" << endl; } return 0; }
This method uses bitwise AND to check the last bit; it's faster but less intuitive for beginners.
Using ternary operator
cpp
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; cout << num << " is " << ((num % 2 == 0) ? "even" : "odd") << endl; return 0; }
This approach is concise and uses a single line to print the result.
Complexity: O(1) time, O(1) space
Time Complexity
The check uses a single modulus operation which takes constant time, so the time complexity is O(1).
Space Complexity
Only a few variables are used, so the space complexity is O(1).
Which Approach is Fastest?
The bitwise AND method is slightly faster than modulus but both are effectively constant time and suitable for this task.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Modulus operator (%) | O(1) | O(1) | Clarity and simplicity |
| Bitwise AND (&) | O(1) | O(1) | Performance and low-level operations |
| Ternary operator | O(1) | O(1) | Concise code |
Use
num % 2 == 0 to quickly check if a number is even in C++.Forgetting to use the modulus operator and trying to check evenness by dividing without checking the remainder.