Friday, February 24, 2017

How to find Even and Odd number in java

There are many ways to find the given number is Even or Odd in any programming languages.Before going to discuss technical details of Even and Odd numbers.
Let's see what is Even and Odd numbers in mathematics.
Even Number: Any Integer that can be divided exactly by 2. The last digit should be – 0,2,4,6,8 Ex: -24,4,16,28
Odd Number: Any integer that cannot be divided exactly by 2. The last digit should be – 1,3,5,7,9 Ex: -13,5,7,17,21
There are different ways to find even and odd number programmatically, we will discuss one by one 

Even or Odd number using modulus (%) operator:

One way is in case of the remainder.Even numbers, the remainder will be 0 and for Odd numbers, the remainder will be 1. We can use this way to find the even and odd number in java. Java had a remainder operator called modulus operation denoted by % which does exactly same, as described above. We will illustrate this example with the program below.
import java.util.Scanner;
public class Manager {
 public static void main(String[] args) {
  try (Scanner scanner = new Scanner(System.in)) {
   System.out.println("Enter the number :");
   int value = scanner.nextInt();
   if (value % 2 == 0) {
    System.out.println("The given number  :: " + value + " is Even Number");
   } else {
    System.out.println("The given number  :: " + value + " is Odd Number");
   }
  } catch (Exception ex) {
   System.out.println("Exception occurred :: " + ex.getMessage());
  }
 }
}

Even or Odd number using bitwise (&) operator:

A bitwise AND takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits, by multiplying them. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0 and 0 × 0 = 0). For example:
0101 (decimal 5)
AND 0011 (decimal 3)
  = 0001 (decimal 1)
The operation may be used to determine whether a particular bit is set (1) or clear (0). For example, given a bit pattern 0011 (decimal 3), to determine whether the second bit is set we use a bitwise AND with a bit pattern containing 1 only in the second bit:
0011 (decimal 3)
AND 0010 (decimal 2)
  = 0010 (decimal 2)
Because the result 0010 is non-zero, we know the second bit in the original pattern was set. This is often called bit masking. (By analogy, the use of masking tape covers, or masks, portions that should not be altered or portions that are not of interest. In this case, the 0 values mask the bits that are not of interest.)
The bitwise AND may be used to clear selected bits (or flags) of a register in which each bit represents an individual Boolean state. This technique is an efficient way to store a number of Boolean values using as little memory as possible.
For example, 0110 (decimal 6) can be considered a set of four flags, where the first and fourth flags are clear (0), and the second and third flags are set (1). The second bit may be cleared by using a bitwise AND with the pattern that has a zero only in the second bit:
0110 (decimal 6)
AND 1101 (decimal 13)
  = 0100 (decimal 4)
Because of this property, it becomes easy to check the parity of a binary number by checking the value of the lowest valued bit. Using the example above:
0110 (decimal 6)
AND 0001 (decimal 1)
  = 0000 (decimal 0)
Because 6 AND 1 is zero, 6 is divisible by two and therefore even.
import java.util.Scanner;
public class Manager{
 public static void main(String[] args) {
  try (Scanner scanner = new Scanner(System.in)) {
   System.out.println("Enter the number :");
   int value = scanner.nextInt();
   if ((value & 1) == 0) {
    System.out.println("The given number  :: " + value + " is Even Number");
   } else {
    System.out.println("The given number  :: " + value + " is Odd Number");
   }
  } catch (Exception ex) {
   System.out.println("Exception occurred :: " + ex.getMessage());
  }
 }
}

Even or Odd number using division (/) operator:

import java.util.Scanner;
public class Manager{
 public static void main(String[] args) {
  try (Scanner scanner = new Scanner(System.in)) {
   System.out.println("Enter the number :");
   int value = scanner.nextInt();
   int quotient = value / 2;
   if (quotient * 2 == value) {
    System.out.println("The given number  :: " + value + " is Even Number");
   } else {
    System.out.println("The given number  :: " + value + " is Odd Number");
   }
  } catch (Exception ex) {
   System.out.println("Exception occurred :: " + ex.getMessage());
  }
 }
}

Now will see few interview question related to Even and Odd numbers in java :

  • How to find given number is even or odd number in java 
  • Find Odd numbers between two numbers in java 
  • Find Even numbers between two numbers in java 
  • Calculate the total number of Odd and Even numbers in java 
  • Print the Even and Odd numbers in an array 

How to find given number is Even or Odd number in java :

import java.util.Scanner;
public class Manager {
 public static void main(String args[]) {
  try (Scanner scanner = new Scanner(System.in)) {
   System.out.println("Check Even or Odd ");
   System.out.println("Enter the number : ");
   int num = scanner.nextInt();
   if ((num % 2) == 0) {
    System.out.println(num + " is Even number");
   } else {
     System.out.println(num + " is Odd number");
   }
  }
 }
}

Find Odd numbers between two numbers in java :

import java.util.Scanner;
public class Manager {
 public static void main(String args[]) {
  try (Scanner scanner = new Scanner(System.in)) {
    System.out.println("Find Odd numbers between two numbers");
     System.out.println("Enter the Starting value : ");
     int start = scanner.nextInt();
     System.out.println("Enter the End value : ");
     int end = scanner.nextInt();
     System.out.println("Odd number:: ");
     for (int i = start; i <= end; i++) {
     if ((i % 2) != 0) {
      System.out.print(i + " ");
     }
    }
  }
 }
}

Find Even numbers between two numbers in java :

import java.util.Scanner;
public class Manager {
 public static void main(String args[]) {
   try (Scanner scanner = new Scanner(System.in)) {
     System.out.println("Find Even numbers between two numbers");
     System.out.println("Enter the Starting value : ");
     int start = scanner.nextInt();
     System.out.println("Enter the End value : ");
     int end = scanner.nextInt();
     System.out.println("Even Numbers:: ");
     for (int i = start; i <= end; i++) {
      if ((i % 2) == 0) {
        System.out.print(i + " ");
      }
    }
   }
  }
}

Calculating the total number of Odd and Even numbers in java:

import java.util.Scanner;
public class Manager {
 public static void main(String args[]) {
  try (Scanner scanner = new Scanner(System.in)) {
  System.out.println("Total number of " + "Odd & Even numbers between two numbers");
   System.out.println("Enter the Starting value : ");
   int start = scanner.nextInt();
   System.out.println("Enter the End value : ");
   int end = scanner.nextInt();
   int odd = 0;
   int even = 0;
   for (int i = start; i <= end; i++) {
    if ((i % 2) == 0) {
     even++;
    } else {
    odd++;
   }
  }
  System.out.println("\nTotal number of Odd number is " + odd);
  System.out.println("Total number of Even number is " + even);
  }
 }
}

Print the Even and Odd number in an array :

Enter the size of an array and then enter all the elements of that array. Now using for loop and if condition we use to distinguish whether given integer in the array is odd or even.Here is the source code of the Java Program to Print the Odd & Even Numbers in an Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner;
public class Manager {
 public static void main(String[] args) {  
  try (Scanner scanner = new Scanner(System.in)) {
   System.out.print("Enter no. of elements you want in array:");
   int number = scanner.nextInt();
   int a[] = new int[number];
   System.out.println("Enter all the elements:");
   for (int i = 0; i < number; i++) {
    a[i] = scanner.nextInt();
   }
   System.out.print("Odd numbers:");
   for (int i = 0; i < number; i++) {
    if (a[i] % 2 != 0) {
     System.out.print(a[i] + " ");
    }
   }
   System.out.println("");
   System.out.print("Even numbers:");
   for (int i = 0; i < number; i++) {
    if (a[i] % 2 == 0) {
     System.out.print(a[i] + " ");
    }
   }
  }
 }
}
Output:
Enter no. of elements you want in array:10
Enter all the elements:
20
27
23
67
48
12
69
25
48
35
Odd numbers:27 23 67 69 25 35 
Even numbers:20 48 12 48 

That's all about How to find Even and Odd numbers in java using different ways. If you find any other ways to find the Even and Odd numbers, Please update your comments below.