![]() |
VOOZH | about |
A number is even if it is completely divisible by 2 and it is odd if it is not completely divisible by 2. In this article, we will learn how to check whether a number is even or odd in C++.
Input: n = 11
Output: Odd
Explanation: Since 11 is not completely divisible by 2, it is an odd number.Input: n = 20
Output: Even
Explanation: Since 20 is completely divisible by 2, it is an even number.
A number can be checked for even or odd using different methods such as the modulo operator, bitwise AND operator, and bitwise shift operations. Each method determines whether a number is divisible by 2 based on its remainder or binary representation.
A number can be classified as even or odd by checking the remainder when it is divided by 2. In C++, the modulo operator (%) is used to find this remainder.
Odd
Time Complexity: O(1)
Auxiliary Space: O(1)
A number can also be checked for even or odd by examining its Least Significant Bit (LSB). In binary representation, odd numbers always have the last bit set to 1, while even numbers have it set to 0.
Odd
Time Complexity: O(1)
Auxiliary Space: O(1)
A number can be checked for even or odd by manipulating its Least Significant Bit (LSB) using bitwise shift operations. Right shifting and then left shifting removes the LSB from the number.
Odd
Time Complexity: O(1)
Auxiliary Space: O(1)