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.
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 != 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>
|
Output :
Odd
Time Complexity: O(1)
Auxiliary Space: O(1)
A better solution is to use bitwise operators. We need to check whether last bit is 1 or not. If last bit is 1 then 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 <stdio.h>
#include <math.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>
|
Output :
Odd
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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.