Java Program to check a given number is Even or Odd

Java Program to check a given number is Even or Odd

In this tutorial, you will be learning to write Java Program to check given number is Even number or odd number.

Even a number is a number that is divisible by 2. And the odd number is a number that is not completely divisible by 2.

For example:

Example 1:

Suppose the given input by the user is 12

then the output will be “Given number is even number”.

Example 2:

Suppose the input given by the user is 19

then the output will be “Given number is an odd number”.

Program 1: Java Program to check a given number is Even or Odd

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        if(num % 2 == 0)
            System.out.println("Given " +num + " is even");
        else
            System.out.println("Given " +num + " is odd");
    }
}

Output

Enter a number: 23
Given 23 is odd

Program 2: Java Program to check a given number is Even or Odd

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        String output = (num % 2 == 0) ? "even" : "odd";
        System.out.println("Given "+num + " is " + output);
    }
}

Output

Enter a number: 22
Given 22 is even