> ## Content Index
> Fetch the complete content index at: https://www.javahandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Challenge 2: Check If a Given Number is Even/Odd
- URL: https://www.javahandbook.com/bitmask/challenge-2-check-if-a-given-number-is-even-odd/
- Published: 2024-06-16T06:55:19.000Z
- Updated: 2024-06-17T03:37:51.000Z
- Author: Gopi Gorantala
- Tags: AND Operator, #bitmask, #docs

This is an example of another Bitwise question. Think of the rightmost bit of a number and think of some logic that could solve this.

### Introduction

This is a classic question in Mathematics 🧮 for computers.

### Example:

```json
Inputs: 1, 3, 5, 7, ... 

Output: Odd
```

```json
Input: 2, 4, 6, 8, ... 

Output: Even
```

### Problem statement

Write a program to check even or odd numbers.

Consider an array of integers here and check each using the AND operator.

```json
Input = {1, 2, 3, 4, 5, 6, 7, 8, 9} 

Output: { "Odd" , "Even" , "Odd" , "Even" , "Odd" , "Even" , "Odd" , "Even" , "Odd" }
```

> **Hint:** Print odd or even using the bitwise operator `&`.

### Thought process

Let’s see how to check if a number is even or odd using the `&` operator.

> In earlier articles, we discussed the bit positions. The right-most significant bit is always *set* or `1` for odd numbers.

Is this hint enough to crack this question?

### Solution

First, we check for `even` and then for `odd`.

Let’s see how to check if a number is even or odd using the `&` operator.

We are checking for an even/odd for a given input.

### Java

```java
class IsEven {
    private static String helper(int n) {
        return (n & 1) == 0 ? "Even" : "Odd";
    }

    public static void main(String[] args) {
        int firstNumber = 125;
        int secondNumber = 8;
        System.out.println("Number '" + firstNumber + "' is : " + helper(firstNumber));
        System.out.println("Number '" + secondNumber + "' is : " + helper(secondNumber));
    }
}
```

### Python

```py
def IsEven(n):
    return "even" if (n & 1)==0 else "odd"

firstNumber=125
secondNumber=8
print("Number ",firstNumber," is : " ,IsEven(firstNumber))
print("Number ",secondNumber," is : " ,IsEven(secondNumber))
```

### JavaScript

```js
const IsEven = n => {
    return (n & 1) === 0 ? 'Even' : 'Odd';
}

const firstNumber = 125;
const secondNumber = 8;
console.log (`Number '${firstNumber}' is : ${IsEven (firstNumber)}`);
console.log (`Number '${secondNumber}' is : ${IsEven (secondNumber)}`);
```

### C++

```cpp
#include <iostream>
#include <string>
using namespace std;

string helper(int n) {
    return (n & 1) == 0 ? "even" : "odd";
}

int main() {
    int firstNumber = 125;
    int secondNumber = 8;
    cout << "Number " << firstNumber << " is : " << helper(firstNumber) << endl;
    cout << "Number " << secondNumber << " is : " << helper(secondNumber);
    return 0;
}
```

### TypeScript

```cpp
export const IsEven = (n: number): string => {
    return (n & 1) === 0 ? 'Even' : 'Odd';
}

const firstNumber: number = 125;
const secondNumber: number = 8;
console.log(`Number '${firstNumber}' is : ${IsEven(firstNumber)}`);
console.log(`Number '${secondNumber}' is : ${IsEven(secondNumber)}`);

```

### Coding exercise

Now, try to optimize the above snippets. There is a better way to solve this problem.

Your solution must use the `&` operator along with considering the right-most significant bit.

This problem is designed for your practice, so try to solve it yourself first. If you get stuck, you can always refer to the solution section's solution. Good luck!

> **Hint:** Try to solve the problem using a simple `&` with `1`.

```java
// java
// TODO: finish the challenge or check next lesson for solution
class Solution{
    public static String isEvenOdd(int number){
        // Write - Your - Code- Here
        
        return ""; // change this, return a String "Even" for even and "Odd" for odd numbers
    }
}
```

```py
# python
# TODO: finish the challenge or check next lesson for solution
def Solution(number):
	# Write - Your - Code- Here
    
    return "" # change this, return a String "Even" for even and "Odd" for odd numbers
```

```js
// javascript
// TODO: finish the challenge or check next lesson for solution
const isEven = n => {
    // Write - Your - Code- Here

    return ""; // change this, return a String "Even" for even and "Odd" for odd numbers
}
```

```cpp
// c++
// TODO: finish the challenge or check next lesson for solution
#include <iostream>
#include <string>
using namespace std;

string isEven(int n) {
  // Write - Your - Code- Here

  return ""; // change this, return a String "Even" for even and "Odd" for odd numbers
}
```

```ts
// typescript
// TODO: finish the challenge or check next lesson for solution
export const isEven = (n: number): string => {
    // Write - Your - Code- Here

    return ""; // change this, return a String "Even" for even and "Odd" for odd numbers
}
```

The solution will be explained in the next lesson.