Given a number, check whether it is even or odd.

Examples :
Input: 2
Output: even
Input: 5
Output: odd
One simple solution is to find the remainder after dividing by 2.
Method 1:
C++
#include <iostream>
using namespace std;
bool isEven( int n) { return (n % 2 == 0); }
int main()
{
int n = 101;
isEven(n) ? cout << "Even" : cout << "Odd" ;
return 0;
}
|
Java
class GFG {
public static boolean isEven( int n)
{
return (n % 2 == 0 );
}
public static void main(String[] args)
{
int n = 101 ;
if (isEven(n) == true )
System.out.print( "Even" );
else
System.out.print( "Odd" );
}
}
|
Python3
def isEven(n):
return (n % 2 = = 0 )
n = 101
print ( "Even" if isEven(n) else "Odd" )
|
C#
using System;
class GFG {
public static bool isEven( int n)
{
return (n % 2 == 0);
}
public static void Main()
{
int n = 101;
if (isEven(n) == true )
Console.WriteLine( "Even" );
else
Console.WriteLine( "Odd" );
}
}
|
PHP
<?php
function isEven( $n )
{
return ( $n % 2 == 0);
}
$n = 101;
if (isEven( $n ) == true)
echo "Even" ;
else
echo "Odd" ;
?>
|
Javascript
<script>
function isEven(n) { return (n % 2 == 0); }
let n = 101;
isEven(n) ? document.write( "Even" ) :document.write( "Odd" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2 :
A better solution is to use bitwise operators. We need to check whether the last bit is 1 or not. If the last bit is 1 then the number is odd, otherwise always even.
Explanation:
input : 5 // odd
00000101
& 00000001
--------------
00000001
--------------
input : 8 //even
00001000
& 00000001
--------------
00000000
--------------
Below is the implementation of the idea.
C++
#include <iostream>
using namespace std;
bool isEven( int n)
{
return (!(n & 1));
}
int main()
{
int n = 101;
isEven(n) ? cout << "Even" : cout << "Odd" ;
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
int main()
{
int n = 101;
if (n % 2 == 0) {
printf ( "Even" );
}
else {
printf ( "Odd" );
}
return 0;
}
|
Java
class GFG {
public static boolean isEven( int n)
{
if ((n & 1 ) == 0 )
return true ;
else
return false ;
}
public static void main(String[] args)
{
int n = 101 ;
if (isEven(n) == true )
System.out.print( "Even" );
else
System.out.print( "Odd" );
}
}
|
Python3
def isEven(n):
return ( not (n & 1 ))
n = 101
print ( "Even" if isEven(n) else "Odd" )
|
C#
using System;
class GFG {
public static bool isEven( int n)
{
if ((n & 1) == 0)
return true ;
else
return false ;
}
public static void Main()
{
int n = 101;
if (isEven(n) == true )
Console.WriteLine( "Even" );
else
Console.WriteLine( "Odd" );
}
}
|
PHP
<?php
function isEven( $n )
{
return (!( $n & 1));
}
$n = 101;
if (isEven( $n ) == true)
echo "Even" ;
else
echo "Odd" ;
?>
|
Javascript
<script>
function isEven(n)
{
return (!(n & 1));
}
let n = 101;
isEven(n)? document.write( "Even" ) :
document.write( "Odd" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 3: Another approach is by using bitwise left-shift and right-shift operators. The logic behind this implementation is about regenerating the value after the right shift and left shift. We all know even numbers have zero as the last bit and odd have one as the last bit. When we bitwise right shift any number then the last bit of the number piped out whenever it is even or odd. Next, we did a bitwise left shift, then our bit shifted by one. Now last bit placed is empty which is by default filled by a zero. During all these odd numbers changed their value but even remains the same. That’s how by comparing the initial and final value we decide number is even or odd.
below is the implementation of the above logic.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 4;
if (a == (a >> 1) << 1) {
cout << "Number is Even: " << a << endl;
}
else {
cout << "Number is Odd: " << a << endl;
}
return 0;
}
|
Java
import java.io.*;
class HelloWorld {
public static void main(String[] args)
{
int a = 4 ;
if (a == (a >> 1 ) << 1 ) {
System.out.printf( "Number %d is even.\n" , a);
}
else {
System.out.printf( "Number %d is not even.\n" ,
a);
}
}
}
|
Python3
a = 4
if a = = (a >> 1 ) << 1 :
print ( "Number is Even:" , a)
else :
print ( "Number is Odd:" , a)
|
C#
using System;
class HelloWorld {
public static void Main( string [] args)
{
int a = 4;
if (a == (a >> 1) << 1) {
Console.WriteLine( "Number " + a + " is even." );
}
else {
Console.WriteLine( "Number " + a
+ " is not even." );
}
}
}
|
Javascript
const a = 4;
if (a == (a >> 1) << 1) {
console.log( "Number is Even: " + a);
}
else {
console.log( "Number is Odd: " + a);
}
|
OutputNumber 4 is even.
Number is Even: 4
Time Complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Prabhat Raushan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.