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.
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&
with1
.
// 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.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.