Given a number, check whether it is even or odd.
Examples :
Input: n = 11
Output: Odd
Input: n = 10
Output: Even
Method 1: Using Loop.
The idea is to start with a boolean flag variable as true and switch it n times. If flag variable gets original value (which is true) back, then n is even. Else n is false.
Below is the implementation of this idea.
C++
#include <iostream>
using namespace std;
bool isEven( int n)
{
bool isEven = true ;
for ( int i=1; i <= n; i++)
isEven = !isEven;
return isEven;
}
int main()
{
int n = 101;
isEven(n) ? cout << "Even" : cout << "Odd" ;
return 0;
}
|
Java
class GFG {
static boolean isEven( int n)
{
boolean isEven = true ;
for ( int i = 1 ; i <= n; i++)
isEven = !isEven;
return isEven;
}
public static void main(String args[])
{
int n = 101 ;
if (isEven(n))
System.out.println( "Even" );
else
System.out.println( "Odd" );
}
}
|
Python3
def isEven(n):
isEven = True ;
for i in range ( 1 , n + 1 ):
if isEven = = True :
isEven = False ;
else :
isEven = True ;
return isEven;
n = 101 ;
if isEven(n) = = True :
print ( "Even" );
else :
print ( "Odd" );
|
C#
using System;
public class GFG {
static bool isEven( int n)
{
bool isEven = true ;
for ( int i = 1; i <= n; i++)
isEven = !isEven;
return isEven;
}
public static void Main()
{
int n = 101;
if (isEven(n))
Console.Write( "Even" );
else
Console.Write( "Odd" );
}
}
|
PHP
<?php
function isEven( $n )
{
$isEven = true;
for ( $i = 1; $i <= $n ; $i ++)
$isEven = ! $isEven ;
return $isEven ;
}
$n = 101;
$is =isEven( $n ) ? "Even" : "Odd" ;
echo "$is"
?>
|
Javascript
<script>
function isEven(n)
{
let isEven = true ;
for (let i = 1; i <= n; i++)
isEven = !isEven;
return isEven;
}
let n = 101;
if (isEven(n))
document.write( "Even" );
else
document.write( "Odd" );
</script>
|
Time Complexity : O(n)
Auxiliary Space: O(1)
Method 2: By multiply and divide by 2. Divide the number by 2 and multiply by 2 if the result is same as input then it is an even number else it is an odd number.
C++
#include <iostream>
using namespace std;
bool isEven( int n)
{
return ((n / 2) * 2 == n);
}
int main()
{
int n = 101;
isEven(n) ? cout << "Even" : cout << "Odd" ;
return 0;
}
|
Java
class GFG {
static boolean isEven( int n)
{
return ((n / 2 ) * 2 == n);
}
public static void main(String[] args)
{
int n = 101 ;
if (isEven(n) != false )
System.out.print( "Even" );
else
System.out.print( "Odd" );
}
}
|
Python3
def isEven(n):
return ( int (n / 2 ) * 2 = = n)
n = 101
if (isEven(n) ! = False ):
print ( "Even" )
else :
print ( "Odd" )
|
C#
using System;
class GFG {
static bool isEven( int n)
{
return ((n / 2) * 2 == n);
}
public static void Main(String[] args)
{
int n = 101;
if (isEven(n) != false )
Console.Write( "Even" );
else
Console.Write( "Odd" );
}
}
|
PHP
<?php
function isEven( $n )
{
return ((int)( $n / 2) * 2 == $n );
}
$n = 101;
if (isEven( $n ))
echo ( "Even" );
else
echo ( "Odd" );
?>
|
Javascript
<script>
function isEven(n)
{
return (parseInt(n / 2, 10) * 2 == n);
}
let n = 101;
isEven(n) ? document.write( "Even" ) :
document.write( "Odd" );
</script>
|
Time Complexity : O(1)
Auxiliary Space: O(1)
Method 3: Using Bitwise operator &.
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 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;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static int isEven( int n)
{
return (n & 1 );
}
public static void main(String args[])
{
int n = 101 ;
if (isEven(n)== 0 )
System.out.print( "Even" );
else
System.out.print( "Odd" );
}
}
|
Python3
def isEven(n) :
return (n & 1 );
n = 101 ;
if (isEven(n) = = 0 ) :
print ( "Even" );
else :
print ( "Odd" );
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int isEven( int n)
{
return (n & 1);
}
public static void Main()
{
int n = 101;
if (isEven(n)==0)
Console.Write( "Even" );
else
Console.Write( "Odd" );
}
}
|
PHP
<?php
function isEven( $n )
{
return ( $n & 1);
}
$n = 101;
if (isEven( $n ) == 0)
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)