how to determine if a number is even or odd in java with modulous code example

Example 1: Java program to check even or odd number

import java.util.Scanner;
public class FindEvenOrOdd
{
   public static void main(String[] args)
   {
      int a;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number to check even or odd: ");
      a = sc.nextInt();
      if(a % 2 == 0)
      {
         System.out.println("Entered number is an even number.");
      }
      else
      {
         System.out.println("Entered number is an odd number.");
      }
      sc.close();
   }
}

Example 2: Check odd or even number in java without using modulus operator

// Check odd or even number in java without using modulus operator
public class EvenOddWithoutModulus
{
   public static void main(String[] args)
   {
      int number = 104;
      if(checkEvenNumber(number))
      {
         System.out.println(number + " is even number.");
      }
      else
      {
         System.out.println(number + "is odd number.");
      }
   }
   static boolean checkEvenNumber(int num)
   {
      boolean boolEven = true;
      for(int a = 1; a <= num; a++)
      {
         boolEven = !boolEven;
      }
      return boolEven;
   }
}

Tags:

Java Example