You are here

Java program to find odd or even

Java program to check if a number is odd or even: if a number is divisible by two then it is even, otherwise, it is odd. We use modulus operator to find remainder in our program. For an even number remainder will be zero when it is divided by two and will be one for an odd number.

Java programming code

  1. import java.util.Scanner;
  2.  
  3. class OddOrEven
  4. {
  5.    public static void main(String args[])
  6.    {
  7.       int x;
  8.       System.out.println("Enter an integer to check if it is odd or even");
  9.       Scanner in = new Scanner(System.in);
  10.       x = in.nextInt();
  11.      
  12.       if (x % 2 == 0)
  13.          System.out.println("The number is even.");
  14.       else
  15.          System.out.println("The number is odd.");
  16.    }
  17. }

Output of program:
odd even

Download Odd or even program class file.

Another method to check odd or even, for explanation see: C program to check odd or even. Code:

  1. import java.util.Scanner;
  2.  
  3. class EvenOdd
  4. {
  5.   public static void main(String args[])
  6.   {
  7.     int c;
  8.     System.out.println("Input an integer");
  9.     Scanner in = new Scanner(System.in);
  10.     c = in.nextInt();
  11.    
  12.     if ((c/2)*2 == c)
  13.       System.out.println("Even");
  14.     else
  15.       System.out.println("Odd");
  16.   }
  17. }

There are other methods for checking odd/even one such way is using bit-wise operators.