Check whether the number is odd or even without using mod operator

It is very simple to find if the given number is either odd or even without using the modulus operator. Even without using the modulus operator, a number can be checked if it is odd or even.

This can be done in the following ways.

Method 1: By using the bitwise (&) operator, a number can be checked if it is odd or even.

Method 2: By multiplying and dividing the number by 2. Divide the number by 2 and multiply it by 2. If the result is the same as that of the input, then it is an even number otherwise, it is an odd number. 

Method 3: By switching temporary variable n times with the initial value of the variable being true. If the flag variable gets its original value (which is true) back, then n is even. Else, n is false.

Program to check if the number is odd or even using bitwise (&) operator

#include <stdio.h>

int isEven(int n)
{
return (!(n & 1));
}

int main()
{
int n;
printf(“\nEnter a number : “);
scanf(“%d”,&n);
printf(“\n”);
printf(“\n%d is “,n);
isEven(n) ? printf(“Even\n”) : printf(“Odd\n”);

return 0;
}

#include <iostream>
using namespace std;

bool isEven(int n)
{
return (!(n & 1));
}

int main()
{
int n;
cout << “\nEnter a number : “;
cin >> n;
cout << endl;
cout << n << ” is “;
isEven(n) ? cout << “Even” : cout << “Odd”;
cout << endl;
return 0;
}

import java.util.*;

public class Main {

static int isEven(int n)
{
return (n & 1);
}


public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println(“Enter the number :”);
n = sc.nextInt();
if(isEven(n) == 0)
System.out.print(“Even\n”);
else
System.out.print(“Odd\n”);
}
}

def isEven(n) :
return (n & 1);

n = int(input(“Enter a number : “))
if(isEven(n) == 0):
print (“Even”)
else :
print (“Odd”)

Program to check if the number is odd or even by divide and multiply by 2 method

#include <stdio.h>

int isEven(int n)
{
return ((n / 2) * 2 == n);
}

int main()
{
int n;
printf(“\nEnter a number : “);
scanf(“%d”,&n);
printf(“\n”);
printf(“\n%d is “,n);
isEven(n) ? printf(“Even\n”) : printf(“Odd\n”);

return 0;
}

#include <iostream>
using namespace std;

bool isEven(int n)
{
return ((n / 2) * 2 == n);
}

int main()
{
int n;
cout << “\nEnter a number : “;
cin >> n;
cout << endl;
cout << n << ” is “;
isEven(n) ? cout << “Even” : cout << “Odd”;
cout << endl;
return 0;
}

import java.util.*;

public class Main {

static boolean isEven(int n)
{
return ((n / 2) * 2 == n);
}


public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println(“Enter the number :”);
n = sc.nextInt();
if(isEven(n))
System.out.print(“Even\n”);
else
System.out.print(“Odd\n”);
}
}

def isEven(n) :
return (int(n / 2) * 2 == n)

n = int(input(“Enter a number : “))
if(isEven(n) != False):
print (“Even”)
else :
print (“Odd”)

Program to check if the number is odd or even by switching a temporary variable

#include <stdio.h>

int isEven(int n)
{
int even_no = 1;
for (int i=1; i <= n; i++)
even_no = !even_no;
return even_no;
}

int main()
{
int n;
printf(“\nEnter a number : “);
scanf(“%d”,&n);
printf(“\n”);
printf(“\n%d is “,n);
isEven(n) ? printf(“Even\n”) : printf(“Odd\n”);

return 0;
}

#include <iostream>
using namespace std;

int isEven(int n)
{
bool even_no = true;
for (int i=1; i <= n; i++)
even_no = !even_no;
return even_no;
}

int main()
{
int n;
cout << “\nEnter a number : “;
cin >> n;
cout << endl;
cout << n << ” is “;
isEven(n) ? cout << “Even” : cout << “Odd”;
cout << endl;
return 0;

import java.util.*;

public class Main {

static boolean isEven(int n)
{
boolean even_no = true;
for (int i=1; i <= n; i++)
even_no = !even_no;
return even_no;
}


public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println(“Enter the number :”);
n = sc.nextInt();
if(isEven(n))
System.out.print(“Even\n”);
else
System.out.print(“Odd\n”);
}
}


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”);

odd or even-even

odd or even-odd