Check Even or Odd Last Updated : 14 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given a number n, check whether it is even or odd. Return true for even and false for odd.Examples: Input: n = 15Output: falseExplanation: 15 % 2 = 1, so 15 is odd .Input: n = 44Output: trueExplanation: 44 % 2 = 0, so 44 is even.Table of Content[Naive Approach] By Finding the Remainder - O(1) Time and O(1) Space[Efficient Approach] Using Bitwise AND Operator - O(1) Time and O(1) Space[Naive Approach] By Finding the Remainder - O(1) Time and O(1) SpaceWe can check the remainder when divided by 2. If the remainder is 0, the number is even, otherwise it is odd. C++ #include <iostream> using namespace std; bool isEven(int n) { // finding remainder of n int rem = n % 2; if(rem == 0){ return true; } else{ return false; } } int main() { int n = 15; if (isEven(n)) cout << "true"; else cout << "false"; return 0; } C #include <stdio.h> #include <stdbool.h> bool isEven(int n) { return n % 2 == 0; } // Driver Code int main() { int n = 15; if (isEven(n)) printf("true\n"); else printf("false\n"); return 0; } Java class GfG { public static boolean isEven(int n) { // finding remainder of n int rem = n % 2; if(rem == 0){ return true; } else{ return false; } } // Driver Code public static void main(String[] args) { int n = 15; if (isEven(n) == true) System.out.print("true"); else System.out.print("false"); } } Python def isEven(n): # finding remainder of n rem = n % 2; if rem == 0: return True else: return False if __name__ == "__main__": n = 15 if isEven(n): print("true") else: print("false") C# using System; class Program { static bool isEven(int n) { // finding remainder of n int rem = n % 2; if (rem == 0){ return true; } else{ return false; } } static void Main() { int n = 15; if (isEven(n)) Console.WriteLine("true"); else Console.WriteLine("false"); } } JavaScript function isEven(n) { // finding remainder of n let rem = n % 2; if(rem == 0){ return true; } else{ return false; } } // Driver code let n = 15; if (isEven(n)) { console.log("true"); } else { console.log("false"); } Outputfalse[Efficient Approach] Using Bitwise AND Operator - O(1) Time and O(1) SpaceThe last bit of all odd numbers is always 1, while for even numbers it’s 0. So, when performing bitwise AND operation with 1, odd numbers give 1, and even numbers give 0.Note: Bitwise operators are extremely fast and efficient because they operate directly at the binary level, making them significantly faster than arithmetic or logical operations.Examples:15 -> 1 1 1 1 & 0 0 0 1 ------- 0 0 0 1 , so this we can say it is an odd number.44 -> 1 0 1 1 0 0 & 0 0 0 0 0 1 ---------- 0 0 0 0 0 0 , so this we can say it is an even number. C++ #include <iostream> using namespace std; bool isEven(int n) { // taking bitwise and of n with 1 if ((n & 1) == 0) return true; else return false; } int main() { int n = 15; if (isEven(n) == true) cout << "true"; else cout << "false"; return 0; } C #include <math.h> #include <stdio.h> #include <stdbool.h> // for using boolean datatype in c bool isEven(int n) { // taking bitwise and of n with 1 if ((n & 1) == 0) return true; else return false; } int main() { int n = 15; if (isEven(n)) { printf("true"); } else { printf("false"); } return 0; } Java class GfG { public static boolean isEven(int n) { // taking bitwise and of n with 1 if ((n & 1) == 0) return true; else return false; } public static void main(String[] args) { int n = 15; if (isEven(n) == true) System.out.print("true"); else System.out.print("false"); } } Python def isEven(n): # taking bitwise and of n with 1 if (n & 1) == 0: return True else: return False if __name__ == "__main__": n = 15 if isEven(n): print("true") else: print("false") C# using System; class GfG { public static bool isEven(int n) { // taking bitwise and of n with 1 if ((n & 1) == 0) return true; else return false; } public static void Main() { int n = 15; if (isEven(n) == true) Console.WriteLine("true"); else Console.WriteLine("false"); } } JavaScript function isEven(n) { // taking bitwise and of n with 1 if ((n & 1) === 0) { return true; } else { return false; } } // Driver Code let n = 15; if (isEven(n)) { console.log("true"); } else { console.log("false"); } Outputfalse Check whether a given number is even or odd Comment More infoAdvertise with us Next Article Check whether a given number is even or odd K kartik Follow Improve Article Tags : Strings Bit Magic Sorting Mathematical DSA +1 More Practice Tags : Bit MagicMathematicalSortingStrings Similar Reads Check if an Octal number is Even or Odd Given an Octal number N, check whether it is even or odd.Examples: Input: N = 7234 Output: Even Input: N = 333333333 Output: Odd Naive Approach: Convert the number from Octal base to Decimal base.Then check if the number is even or odd, which can be easily checked by dividing by 2. Time Complexity: 4 min read Check if an Octal number is Even or Odd Given an Octal number N, check whether it is even or odd.Examples: Input: N = 7234 Output: Even Input: N = 333333333 Output: Odd Naive Approach: Convert the number from Octal base to Decimal base.Then check if the number is even or odd, which can be easily checked by dividing by 2. Time Complexity: 4 min read Check if an Octal number is Even or Odd Given an Octal number N, check whether it is even or odd.Examples: Input: N = 7234 Output: Even Input: N = 333333333 Output: Odd Naive Approach: Convert the number from Octal base to Decimal base.Then check if the number is even or odd, which can be easily checked by dividing by 2. Time Complexity: 4 min read Check whether a given number is even or odd Given a number n, check whether it is even or odd. Return true for even and false for odd.Examples: Input: n = 15Output: falseExplanation: 15 % 2 = 1, so 15 is odd .Input: n = 44Output: trueExplanation: 44 % 2 = 0, so 44 is even.Table of Content[Naive Approach] By Finding the Remainder - O(1) Time a 4 min read Check whether a given number is even or odd Given a number n, check whether it is even or odd. Return true for even and false for odd.Examples: Input: n = 15Output: falseExplanation: 15 % 2 = 1, so 15 is odd .Input: n = 44Output: trueExplanation: 44 % 2 = 0, so 44 is even.Table of Content[Naive Approach] By Finding the Remainder - O(1) Time a 4 min read Check whether a given number is even or odd Given a number n, check whether it is even or odd. Return true for even and false for odd.Examples: Input: n = 15Output: falseExplanation: 15 % 2 = 1, so 15 is odd .Input: n = 44Output: trueExplanation: 44 % 2 = 0, so 44 is even.Table of Content[Naive Approach] By Finding the Remainder - O(1) Time a 4 min read Check Even or Odd min read Like