**Java programs to check if a number is even or odd** have been shown here. An integer number $n$ is said to be even if it is divisible by $2$ i.e. $(n\mod 2 = 0)$ and it is called odd if it is not divisible by 2 i.e. it does not result in 0 as the remainder when divided by $2$. So a number in the form of $2n$, where $n$ is an integer, is called even and a number in the form of $2n+1$ or $2n-1$ is called odd. For example $... -4, -2, 0, 2, 4$ ... etc. are even numbers and $... -3, -1, 1, 3, 5$ ... etc. are odd numbers.

Another approach is to apply bitwise AND operator (&) to check if $n$ is even or odd. We need to perform bitwise AND operation between $n$ and $1$. If $n$ AND $1 = 0$, then $n$ is even and if $n$ AND $1 = 1$, then $n$ is odd.

Page content(s):

**4. Program & output: Modulo operation**

**5. Program & output: Bitwise AND operation**

Additional content(s):

## 1. Algorithm to check if a number is even or odd

// $n$ is an integer number as input//

1. Check if $n \mod 2 = 0$, then Return *Even* else

2. Return *Odd*

## 2. Pseudocode to check if a number is even or odd

**Input**: An integer number $n$

**Output**: If $n$ is even or odd

1. **Procedure** evenOrOdd($n$):

2. **If** $n\mod2==0$:

3. **Return ***even*

4. **Else:**

5. **Return ***odd*

6. **End Procedure**

## 3. Time complexity to check if a number is even or odd

**Time Complexity: ****O(1)**

## 4. Java Program to check if a number is even or odd

/************************************************** alphabetacoder.com Java program to check if a number is even or odd ***************************************************/ import java.util.Scanner; public class Main { public static void main(String args[]) { // declare variable int n; // declare an instance of scanner class Scanner sc = new Scanner(System.in); //take input of a number System.out.print("Enter the number = "); n = sc.nextInt(); //check if n is even or odd if (n % 2 == 0) System.out.print(n + " is even"); else System.out.print(n + " is odd"); } }

Output

**Case 1:**

Enter the number = -5

-5 is odd

**Case 2:**

Enter the number = 8

8 is even

**Case 3:**

Enter the number = 3

3 is odd

## 5. Java Program to check if a number is even or odd using bitwise AND operation

/*************************************** alphabetacoder.com Java program to check if a number is even or odd using bitwise AND operation ****************************************/ import java.util.Scanner; public class Main { public static void main(String args[]) { // declare variable int n; // declare an instance of scanner class Scanner sc = new Scanner(System.in); //take input of a number System.out.print("Enter the number = "); n = sc.nextInt(); //check if n is even or odd // by doing bitwise AND operation // with 1. If the result is 0 then // n is even. If the result is 1 // then n is odd. if ((n & 1) == 0) System.out.print(n + " is even"); else System.out.print(n + " is odd"); } }

Output

**Case 1:**

Enter the number = 15

15 is odd

**Case 2:**

Enter the number = 22

22 is even

**Case 3:**

Enter the number = 13

13 is odd