Java Program to Check Even or Odd Numbers

In this program, we will learn to code the Java Program to Check Even or Odd Numbers. Let’s understand more about even and odd numbers then we will see the logic to check whether a number is even or odd in Java Programming Language.

Even Numbers

A whole number that can be divided by 2 into two whole numbers is called an even number. For example, 2, 4, 6, 8, 10, etc.

Odd Numbers

A whole number that cannot be divided by 2 into two whole numbers is called an even number. For example, 1, 3, 5, 7, 9, etc.

Now, let’s see the Java Program to Check Even or Odd Numbers.

Java Program to Check Even or Odd Numbers

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a Number : ");
        int num = sc.nextInt();
        
        //Logic to Check if the number is even or odd
        if(num == 0){
            System.out.println("Number is 0 which is neither Even nor Odd.");
        }else if(num % 2 != 0){
            System.out.println(""+num+" is an ODD Number");
        }else{
            System.out.println(""+num+" is an EVEN Number");
        }
    }
    
}

Output

Enter a Number : 123

123 is an ODD Number

Output

Enter a Number : 12

12 is an EVEN Number

How does this program work?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a Number : ");
        int num = sc.nextInt();

In this program, we have first taken the input of the number using the Scanner Class in Java.

         //Logic to Check if the number is even or odd
        if(num == 0){
            System.out.println("Number is 0 which is neither Even nor Odd.");
        }else if(num % 2 != 0){
            System.out.println(""+num+" is an ODD Number");
        }else{
            System.out.println(""+num+" is an EVEN Number");
        }

Then, we have used the if-else-if ladder to check whether the number is fully divisible by 2 or not.
If the remainder is 0 when the given number is divided by 2 then it is an Even Number otherwise it is an Odd Number. So, to check the remainder we use the modulo operator i.e. “%”.

This is the Java Program to Check Even or Odd Numbers.

Conclusion

I hope after going through this post, you understand the Java Program to Check Even or Odd Numbers.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *