Thursday, June 18, 2020

Even or Odd Program

C programs to check odd or even using different methods. In the decimal number system, even numbers are exactly divisible by two while odd numbers are not. We can use modulus operator '%' which returns the remainder, for example, 4%3 = 1 (remainder when four is divided by three

Odd or even program in C using modulus operator

#include <stdio.h>
int main()
{
  int n;
  printf("Enter an integer\n");
  scanf("%d", &n);
  if (n%2 == 0)
    printf("Even\n");
  else
    printf("Odd\n");
  return 0;
}
We can use bitwise AND (&) operator to check odd or even. For example, consider binary of 7 (0111), (7 & 1 = 1). You may observe that the least significant bit of every odd number is 1. Therefore (odd_number & 1) is one always and also (even_number & 1) is always zero.

C program to find odd or even using bitwise operator

#include <stdio.h>
int main()
{
  int n;
 
  printf("Input an integer\n");
  scanf("%d", &n);
  if (n & 1 == 1)
    printf("Odd\n");
  else
    printf("Even\n");
 
  return 0;
}
Another Way:
//আমি আরেকটু শর্টকাট করি নিচের মত করে

if(num & 1)
    printf("odd");
else
    printf("even);
Another Way Less Efficient Code:
if ((n & -n) == 1)
    printf("odd");
else
    printf("even");

Another Way: Using XOR 
if ( (num ^ 1) < num)
    printf("odd");
else
    printf("even");

3. Using Bitwise AND Operator (&)

Bitwise operators work on individual bits of a variable. For example, when you perform an operation on 5 and 1 (5 & 1), the result is 1. Let's see how it works:
 5       - 0101  ( 5 in binary form ) - 5 is odd number here.
 1       - 0001  ( 1 in binary form )
5 & 1    - 0001  ( result after performing bitwise & i.e 1)
If you see above, after performing bitwise on 5 and 1, we got result 1. Let's perform bitwise & on 4 (even number ) and 1.
4        - 0100 ( 4 in binary form) - 4 is even number here.
1        - 0001 ( 1 in binary form) - 1 is even number here.
4 & 1    - 0000 ( result is 0)
The rules of Bitwise & when performed on bits 0 and 1.
0 & 0 - 0
0 & 1 - 0
1 & 0 - 0
1 & 1 - 1
Now, let's go through the solution, if you see in the above examples when you perform bitwise & on an odd number and 1, we got result 1, when you perform on even number and 1, it is 0, which means:
( odd number ) & 1 is 1.
( even number ) & 1 is 0.
So the algorithm is:
if( number & 1 == 0) {
   even number
} else {
   odd number
}
The code snippet is below:
private void isEvenM3(int i) {
       
        int res = i & 1;
       
        if(res == 0) {
            System.out.println(i +" is Even Number");
        } else {
            System.out.println(i + " is Not Even Number");
        }
    }

4. Using Left Shift and Right Shift Operators (<< , >>)

Similar to Bitwise operators, Left shift and Right shift operators work on individual bits of a number. Let's see how Left shift operator and right shift operator work. For example:
5 >> 1 - (0101) >> 1 = 0010, i.e., 2
Here we performed right shift once, i.e., 5 in binary form is 0101 and doing right shift once, it becomes 0010, as right most digit goes away. Therefore 5 >> 1 is 2. Now let's see how left shift operator works:
5 << 1 - ( 0101 ) << 1 = 1010 i.e.,10
Here, when you do left shift of 1 bit, the left most bit goes away.
Let's see one more example, now take 4 for our purpose.
4 >> 1 - ( 0100 ) >> 1 = 2.
4 << 1 - ( 0100 ) << 1 = 8.
Now we understand how left shift and right shift operators works. Now let's see how to solve our problem using left and right shift operators. Now take 4, 5 for our reference.
Now I am going to perform one right shift and one left shift on both 4 and 5.
( 4 >> 1 ) << 1 = ( 2 ) << 1 = 4.
( 5 >> 1 ) << 1 = ( 2 ) << 1 = 4.
If you observe above, for even numbers, we are getting the same number again, but for odd numbers, we are getting a different number. We can make use of this algorithm to solve our problem.
if ( ( num >> 1) << 1 == num) {
    even number
} else {
    odd number
}
The code snippet is below:
private void isEvenM4(int i) {
        int res = (i >> 1) << 1; // right by 1 bit and then left shift by 1 bit
        if(res == i) {
            System.out.println(i +" is Even Number");
        } else {
            System.out.println(i + " is Not Even Number");
        }       
    }

Example 2: Check Whether Number is Even or Odd using ternary operators

#include <iostream>
using namespace std;

int main()
{
    int n;

    cout << "Enter an integer: ";
    cin >> n;
    
    (n % 2 == 0) ? cout << n << " is even." :  cout << n << " is odd.";
    
    return 0;
}

No comments:

Post a Comment