Java program to find whether given number is even or odd

Java Programming Java8Java Technologies

An even number is a number which is divisible by 2. Therefore, perform %2 operation on the given number, if the result is 0 then the given number is even else given number is odd.

Example

import java.util.Scanner;
public class EvenOrOdd {
   public static void main(String args[]){
      //Reading number from user
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a number ::" );
      int num = sc.nextInt();
     
      //Finding even or odd
      if (num % 2 == 0){
         System.out.println("Entered number is an even number");
      } else {
         System.out.println("Entered number is an odd number");
      }
   }
}

Output1

Enter a number ::
9
Entered number is an odd number

Output2

Enter a number ::
8
Entered number is an even number
raja
Published on 25-Apr-2018 11:39:09
Advertisements