Even odd number checker program in Java

Even odd number checker program in Java

Java is one of the most popular programming languages in the world. It has been over two decades since its first release and is now used by a million developers. It is a fast, secure, and reliable programming language with applications ranging from web, and mobile apps to gaming, and the Internet of Things. Before building industry-level applications, we need to have basic knowledge of the language. Keeping this in mind, let’s learn how to code an even-odd checker program in Java.

Java is platform-independent which allows us to run Java files on any operating system such as windows, mac, and Linux. This is possible with the help of JVM, i.e Java Virtual Machine. 

Odd and Even

Odd numbers are whole numbers that cannot be divided exactly into pairs. For example 5, if we divide the number 5 by 2, we would get 1 as the remainder. Whereas, even numbers are numbers that are exactly divisible by 2 like 0, 2, 4, 6, etc.

Program using if else

Let’s write a program to check whether a number is odd or even. We’ll follow the same approach by checking whether the number is divisible by 2 or not.

import java.util.Scanner;
 
public class Oddeven {
 
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
 
        System.out.print("Enter number: ");
        int num = sc.nextInt();
 
        if (num % 2 == 0)
            System.out.println(num + " is even");
        else
            System.out.println(num + " is odd");
 
        sc.close();
    }
}
Code language: JavaScript (javascript)
oddeven output
Output

In the above code, we have imported the necessary packages required to run our odd-even checker program. Since we want to check our number, we’ll be taking input from the user. This can be done using the Scanner class, which we imported before our Oddeven class.

Note: Run this program with the filename Oddeven.java.

Our primary class is the Oddeven class,  in which we have defined the main(). First, we take an integer input from the user. We want to check whether the number is odd or even. Therefore, we use the % operator to get the remainder when the number is divided by 2. If the remainder turns out to be zero, we can clearly say that the number is even, else the number is odd. 

Program using ternary operator 

import java.util.Scanner;
public class Oddeven {
 
   public static void main(String[] args) {
 
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter number: ");
    int num = sc.nextInt();
 
    String result=(num % 2 == 0) ? "even" : "odd";
    System.out.println(num + " is " + result);
    sc.close();
    }
}
Code language: JavaScript (javascript)

In this program, we’ll be using the ternary operator to find whether the number is even or odd. The ternary operator has the following syntax: (condition) ? (operation 1) : (operation 2)

If the condition is true, then operation 1 will take place else operation 2 will be performed. For example, the number 3, 3%2 will be equal to 1, therefore "odd" is stored in the String result. Then, we will print the subsequent output.

Program using bitwise operators

Bitwise operators are used in executing bitwise operations on binary numerals that involve the manipulation of individual bits. In the above code, we are using the bitwise & operator. The & operator behaves with the following inputs as follows:

AND table:

Operand 1Operand 2Result
000
010
100
111
AND table

For example, 5 in binary can be interpreted as 101. Therefore, when we perform 5&1 operation, we are actually performing 101&1 binary. This operation results in 1. If we take an even number like 2, i.e 10 in binary, the result of 2&1 results in 0. Thus, using the bitwise & operator, we can check whether the number is odd or even. 

import java.util.Scanner;
 
public class Oddeven {
 
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
 
        System.out.print("Enter number: ");
        int num = sc.nextInt();
 
        int ans = num & 1;
 
        if (ans == 0)
            System.out.println(num + " is even");
        else
            System.out.println(num + " is odd");
        sc.close();
    }
}
Code language: JavaScript (javascript)

We can also calculate odd-even using OR operator, where ans = num|0. Since, ans will become 0 only if num in binary ends with a zero, i.e an even number.


These are some of the ways by which we can check whether the number is odd or even. You can start learning about Java from Codedamn by clicking here.

Sharing is caring

Did you like what Fidal Mathew wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far