 # Even odd number checker program in Javaself.__wrap_b=(t,n,e)=>{e=e||document.querySelector(`[data-br="\${t}"]`);let s=e.parentElement,r=R=>e.style.maxWidth=R+"px";e.style.maxWidth="";let o=s.clientWidth,i=s.clientHeight,c=o/2-.25,l=o+.5,u;if(o){for(;c+1<l;)u=Math.round((c+l)/2),r(u),s.clientHeight===i?l=u:c=u;r(l*n+o*(1-n))}e.__wrap_o||(e.__wrap_o=new ResizeObserver(()=>{self.__wrap_b(0,+e.dataset.brr,e)})).observe(s)};self.__wrap_b(":R15dol6:",1) 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.

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```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)```

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:

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.

Learn to code interactively - without ever leaving your browser.