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.

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

ggorantala Twitter

Gopi has a decade of experience with a deep understanding of Java, Microservices, and React. He worked in India & Europe for startups, the EU government, and tech giants.

Comments


Related Posts

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

Members Public

Find Even Number Of Digits in an Array

This problem tests your knowledge of mathematics. Solving this problem helps you find the place values and how they are represented in the decimal number system.

Members Public

Array Strengths, Weaknesses, and Big-O Complexities

In this lesson, you will learn about array strengths and weaknesses along with the big-o complexities.