Skip to content

Challenge 2: Check If a Given Number is Even/Odd

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.

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

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:

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

Output: Odd
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.

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

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

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

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++

#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

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
// 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
    }
}
# 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
// 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
}
// 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
}
// 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.

Coding Interview QuestionsData Structures and AlgorithmsBit Manipulation

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Leetcode 217: Contains Duplicate

This question marks the first problem when working on duplicate data, either integers or strings etc. Companies that have asked this in their coding interview are Amazon, Apple, Netflix, Google, Microsoft, Adobe, Facebook, and many more top tech companies. Problem statement Given an integer array nums, return true if any

Leetcode 217: Contains Duplicate
Members Public

Leetcode 121: Best Time To Buy and Sell Stock

The Best time to buy and sell stock problem is a classic problem that can be solved using the Greedy approach. This is one of the most popular questions asked in such interviews. Companies that have asked this in their coding interview are Facebook, Amazon, Apple, Netflix, Google, Microsoft, Adobe,

Leetcode 121: Best Time To Buy and Sell Stock
Members Public

Bit Manipulation Course Overview

Overview In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills. This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies. To kick things