Posted on by Kalkicode
Code Bit Logic

# Check a number is even or odd without using arithmetic operators

The given problem is to determine whether a given number is even or odd without using any arithmetic operators (e.g., modulo, division, etc.). We need to come up with a solution that relies on bitwise operations instead of conventional arithmetic calculations.

## Explanation with Suitable Example

Let's understand the approach through an example. Consider the number `18` in binary representation:

``18 (Decimal) = 10010 (Binary)``

To determine if this number is even or odd using bitwise operations, we can use the fact that even numbers have their least significant bit (LSB) set to `0`, while odd numbers have their LSB set to `1`. The LSB is the rightmost bit of a binary representation.

## Pseudocode

1. Read the input number `num`.
2. Perform a bitwise AND operation between `num` and `1` (binary representation: `0001`).
3. If the result of the bitwise AND is `0`, then the number is even. Otherwise, it is odd.
4. Print the result accordingly.

## Algorithm

``````checkEvenOdd(num):
if (num & 1) is 0:
print "Is Even Number"
else:
print "Is Odd Number"
``````

## Explanation

1. We take the input number `num`.
2. We perform a bitwise AND operation between `num` and `1` (`num & 1`).
3. The bitwise AND operation will only preserve the LSB of `num` and set all other bits to `0`.
4. If the result of the bitwise AND operation is `0`, it means the LSB of `num` is `0`, indicating that the number is even. In this case, we print "Is Even Number".
5. If the result of the bitwise AND operation is `1`, it means the LSB of `num` is `1`, indicating that the number is odd. In this case, we print "Is Odd Number".

## Code Solution

``````// C Program
// Check a number is even or odd without using arithmetic operators
#include <stdio.h>
// Determine whether given number is Even or Odd
void checkEvenOdd(int num)
{
// Display given number
printf("\n Number : %d",num);

if((num & 1 ) == 0 )
{
// When last bit is not set
printf("\n Is Even Number \n");
}
else
{
printf("\n Is Odd Number\n");
}
}
int main(int argc, char const *argv[])
{

// Test Cases
// 12 (1100)
checkEvenOdd(12);
// 34 (100010)
checkEvenOdd(34);
// -12 (∞11110100)
checkEvenOdd(-12);
// 7 (111)
checkEvenOdd(7);
// 21 (00010101)
checkEvenOdd(21);
// -11 (11110101)
checkEvenOdd(-11);
return 0;
}``````

#### Output

`````` Number : 12
Is Even Number

Number : 34
Is Even Number

Number : -12
Is Even Number

Number : 7
Is Odd Number

Number : 21
Is Odd Number

Number : -11
Is Odd Number``````
``````/*
Java Program
Check a number is even or odd without using arithmetic operators
*/
public class Comparison
{
// Determine whether given number is Even or Odd
public void checkEvenOdd(int num)
{
// Display given number
System.out.print("\n Number : " + num );
if ((num & 1) == 0)
{
// When last bit is not set
System.out.print("\n Is Even Number \n");
}
else
{
System.out.print("\n Is Odd Number\n");
}
}
public static void main(String[] args)
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
// -11 (11110101)
}
}``````

#### Output

`````` Number : 12
Is Even Number

Number : 34
Is Even Number

Number : -12
Is Even Number

Number : 7
Is Odd Number

Number : 21
Is Odd Number

Number : -11
Is Odd Number``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Check a number is even or odd without using arithmetic operators
*/
class Comparison
{
public:
// Determine whether given number is Even or Odd
void checkEvenOdd(int num)
{
// Display given number
cout << "\n Number : " << num;
if ((num &1) == 0)
{
// When last bit is not set
cout << "\n Is Even Number \n";
}
else
{
cout << "\n Is Odd Number\n";
}
}
};
int main()
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
// -11 (11110101)
return 0;
}``````

#### Output

`````` Number : 12
Is Even Number

Number : 34
Is Even Number

Number : -12
Is Even Number

Number : 7
Is Odd Number

Number : 21
Is Odd Number

Number : -11
Is Odd Number``````
``````// Include namespace system
using System;
/*
C# Program
Check a number is even or odd without using arithmetic operators
*/
public class Comparison
{
// Determine whether given number is Even or Odd
public void checkEvenOdd(int num)
{
// Display given number
Console.Write("\n Number : " + num);
if ((num & 1) == 0)
{
// When last bit is not set
Console.Write("\n Is Even Number \n");
}
else
{
Console.Write("\n Is Odd Number\n");
}
}
public static void Main(String[] args)
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
// -11 (11110101)
}
}``````

#### Output

`````` Number : 12
Is Even Number

Number : 34
Is Even Number

Number : -12
Is Even Number

Number : 7
Is Odd Number

Number : 21
Is Odd Number

Number : -11
Is Odd Number``````
``````<?php
/*
Php Program
Check a number is even or odd without using arithmetic operators
*/
class Comparison
{
// Determine whether given number is Even or Odd
public	function checkEvenOdd(\$num)
{
// Display given number
echo "\n Number : ". \$num;
if ((\$num & 1) == 0)
{
// When last bit is not set
echo "\n Is Even Number \n";
}
else
{
echo "\n Is Odd Number\n";
}
}
}

function main()
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
// -11 (11110101)
}
main();``````

#### Output

`````` Number : 12
Is Even Number

Number : 34
Is Even Number

Number : -12
Is Even Number

Number : 7
Is Odd Number

Number : 21
Is Odd Number

Number : -11
Is Odd Number``````
``````/*
Node Js Program
Check a number is even or odd without using arithmetic operators
*/
class Comparison
{
// Determine whether given number is Even or Odd
checkEvenOdd(num)
{
// Display given number
process.stdout.write("\n Number : " + num);
if ((num & 1) == 0)
{
// When last bit is not set
process.stdout.write("\n Is Even Number \n");
}
else
{
process.stdout.write("\n Is Odd Number\n");
}
}
}

function main()
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
// -11 (11110101)
}
main();``````

#### Output

`````` Number : 12
Is Even Number

Number : 34
Is Even Number

Number : -12
Is Even Number

Number : 7
Is Odd Number

Number : 21
Is Odd Number

Number : -11
Is Odd Number``````
``````#   Python 3 Program
#   Check a number is even or odd without using arithmetic operators

class Comparison :
#  Determine whether given number is Even or Odd
def checkEvenOdd(self, num) :
#  Display given number
print("\n Number : ", num, end = "")
if ((num & 1) == 0) :
#  When last bit is not set
print("\n Is Even Number ")
else :
print("\n Is Odd Number")

def main() :
#  Test Cases
#  12 (1100)
#  34 (100010)
#  -12 (∞11110100)
#  7 (111)
#  21 (00010101)
#  -11 (11110101)

if __name__ == "__main__": main()``````

#### Output

`````` Number :  12
Is Even Number

Number :  34
Is Even Number

Number :  -12
Is Even Number

Number :  7
Is Odd Number

Number :  21
Is Odd Number

Number :  -11
Is Odd Number``````
``````#   Ruby Program
#   Check a number is even or odd without using arithmetic operators

class Comparison
#  Determine whether given number is Even or Odd
def checkEvenOdd(num)
#  Display given number
print("\n Number : ", num)
if ((num & 1) == 0)
#  When last bit is not set
print("\n Is Even Number \n")
else
print("\n Is Odd Number\n")
end

end

end

def main()
#  Test Cases
#  12 (1100)
#  34 (100010)
#  -12 (∞11110100)
#  7 (111)
#  21 (00010101)
#  -11 (11110101)
end

main()``````

#### Output

`````` Number : 12
Is Even Number

Number : 34
Is Even Number

Number : -12
Is Even Number

Number : 7
Is Odd Number

Number : 21
Is Odd Number

Number : -11
Is Odd Number
``````
``````/*
Scala Program
Check a number is even or odd without using arithmetic operators
*/
class Comparison
{
// Determine whether given number is Even or Odd
def checkEvenOdd(num: Int): Unit = {
// Display given number
print("\n Number : " + num);
if ((num & 1) == 0)
{
// When last bit is not set
print("\n Is Even Number \n");
}
else
{
print("\n Is Odd Number\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Comparison = new Comparison();
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
// -11 (11110101)
}
}``````

#### Output

`````` Number : 12
Is Even Number

Number : 34
Is Even Number

Number : -12
Is Even Number

Number : 7
Is Odd Number

Number : 21
Is Odd Number

Number : -11
Is Odd Number``````
``````/*
Swift 4 Program
Check a number is even or odd without using arithmetic operators
*/
class Comparison
{
// Determine whether given number is Even or Odd
func checkEvenOdd(_ num: Int)
{
// Display given number
print("\n Number : ", num, terminator: "");
if ((num & 1) == 0)
{
// When last bit is not set
print("\n Is Even Number ");
}
else
{
print("\n Is Odd Number");
}
}
}
func main()
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
// -11 (11110101)
}
main();``````

#### Output

`````` Number :  12
Is Even Number

Number :  34
Is Even Number

Number :  -12
Is Even Number

Number :  7
Is Odd Number

Number :  21
Is Odd Number

Number :  -11
Is Odd Number``````
``````/*
Kotlin Program
Check a number is even or odd without using arithmetic operators
*/
class Comparison
{
// Determine whether given number is Even or Odd
fun checkEvenOdd(num: Int): Unit
{
// Display given number
print("\n Number : " + num);
if ((num and 1) == 0)
{
// When last bit is not set
print("\n Is Even Number \n");
}
else
{
print("\n Is Odd Number\n");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
// -11 (11110101)
}``````

#### Output

`````` Number : 12
Is Even Number

Number : 34
Is Even Number

Number : -12
Is Even Number

Number : 7
Is Odd Number

Number : 21
Is Odd Number

Number : -11
Is Odd Number``````
``````// Rust Program
// Check a number is even or odd without using arithmetic operators
fn main()
{
// Test Cases
// 12 (1100)
check_even_odd(12);
// 34 (100010)
check_even_odd(34);
// -12 (∞11110100)
check_even_odd(-12);
// 7 (111)
check_even_odd(7);
// 21 (00010101)
check_even_odd(21);
// -11 (11110101)
check_even_odd(-11);
}
// Determine whether given number is Even or Odd
fn check_even_odd(num: i32)
{
// Display given number
print!("\n Number : {}", num);
if (num & 1) == 0
{
// When last bit is not set
print!("\n Is Even Number \n");
}
else
{
print!("\n Is Odd Number\n");
}
}``````

#### Output

`````` Number : 12
Is Even Number

Number : 34
Is Even Number

Number : -12
Is Even Number

Number : 7
Is Odd Number

Number : 21
Is Odd Number

Number : -11
Is Odd Number``````

## Resultant Output Explanation

• For the test cases provided in the code:
1. `checkEvenOdd(12)` - The binary representation of `12` is `1100`, and since the LSB is `0`, the number is even. Output: "Is Even Number".
2. `checkEvenOdd(34)` - The binary representation of `34` is `100010`, and since the LSB is `0`, the number is even. Output: "Is Even Number".
3. `checkEvenOdd(-12)` - The binary representation of `-12` (assuming 8-bit two's complement) is `11110100`, and since the LSB is `0`, the number is even. Output: "Is Even Number".
4. `checkEvenOdd(7)` - The binary representation of `7` is `111`, and since the LSB is `1`, the number is odd. Output: "Is Odd Number".
5. `checkEvenOdd(21)` - The binary representation of `21` is `10101`, and since the LSB is `1`, the number is odd. Output: "Is Odd Number".
6. `checkEvenOdd(-11)` - The binary representation of `-11` (assuming 8-bit two's complement) is `11110101`, and since the LSB is `1`, the number is odd. Output: "Is Odd Number".

## Time Complexity of the Code

The time complexity of this code is O(1) because the operations performed are independent of the input size. The bitwise AND operation takes constant time as it is performed on the fixed-size binary representation of the input number. Therefore, the time complexity remains constant regardless of the input magnitude.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post