# Even Odd Program in Java

Write a Java Even Odd Program to show you, How to check whether the given number is Odd or Even number using the If Statement and Conditional Operator with an example. If the number is divisible by 2, it is an even number, and the remaining (not divisible by 2) are odd numbers.

The Java Programming has a % (Module) Arithmetic Operator to check the remainder. If the remainder is 0, then the number is even otherwise, an odd number.

## Java Even Odd Program using IF Condition

This Java even odd program allows the user to enter any integer value. Then this Java program checks whether that number is even or odd using Java If statement.

```package SimpleNumberPrograms;

import java.util.Scanner;

public class EvenorOddUsingIf {
private static Scanner sc;

public static void main(String[] args) {
int Number;
sc = new Scanner(System.in);
System.out.println("\n Please Enter any integer Value: ");
Number = sc.nextInt();

if (Number % 2 == 0) {
System.out.println("\n You have entered EVEN Number");
}
else {
System.out.println("\n You have entered ODD Number");
}
}
}```

Within the Java even or odd example, the following statement will ask the user to Enter any integer value to check whether it is Even or Odd.

`System.out.println("\n Please Enter any integer Value: ");`

The below statement will assign the user entered value to a number variable.

`Number = sc.nextInt();`

In the Next line, We declared the If Else statement to check even or odd in Java

`if (Number % 2 == 0) {`

As we all know, the number that is completely divisible by 2 is even number. So, we used the If condition to check whether the remainder of the number divided by 2 is exactly equal to 0 or not.

• If the Java If statement is True, the statement inside the If block will print. That is Even number
• If the condition is False, the Java Programming Else block statement will print. That is Odd number

We have entered the value as 48 to check, and the Java program is returning the even number as output

## Even Odd Program in Java using Conditional Operator

This Java program checks whether the number is even or odd using the Java Ternary operator. Let’s see the example.

```package SimpleNumberPrograms;

import java.util.Scanner;

public class EOOUsingConditional {
private static Scanner sc;

public static void main(String[] args) {
int Number;
sc = new Scanner(System.in);
System.out.println("\n Please Enter the First integer Value: ");
Number = sc.nextInt();

System.out.println((Number%2)==0? "\n You have entered EVEN Number":
"\n You have entered ODD Number");
}
}```

From the below Java Even Odd Program screenshot, you can observe that we entered 19 as input. As you can see, it is returning Odd Number as Output

`````` Please Enter the First integer Value:
19

You have entered ODD Number``````

You can observe from the above Java Even Odd Program code that, we used if condition to check whether the remainder of the given integer divisible by 2 is 0 or not and print as per the result.

Let us enter 28 to check the other output

`````` Please Enter the First integer Value:
28

You have entered EVEN Number``````

## Even Odd Program in Java using Methods

In this Java program, we are using the same steps that we followed in our first example. However, in this Java program, we separated the logic to find even or odd and placed in the separate method.

```package SimpleNumberPrograms;

import java.util.Scanner;

public class EOOUsingMethods {

private static Scanner sc;

public static void main(String[] args) {
int Number;
sc = new Scanner(System.in);
System.out.println("\n Please Enter the First integer Value: ");
Number = sc.nextInt();

evOrOd(Number);

}
public static void evOrOd(int Number) {
if (Number % 2 == 0) {
System.out.println("\n You have entered EVEN Number");
}
else {
System.out.println("\n You have entered ODD Number");
}
}
}```

Java even odd output

`````` Please Enter the First integer Value:
56

You have entered EVEN Number``````

In this Java Odd Even Program, If you observe the following statement, we are calling the evOrOd method

`evOrOd(Number);`

From the above code snippet inside the evOrOd method, you can observe that this method accepts one argument of integer type. Within the function, we are performing the same operation that we explained in our previous example.

## Java Even Odd Program using OOPs

In this program, we are dividing the code using the Object-Oriented Programming. To do this, First, we will create a class that holds two methods.

```package SimpleNumberPrograms;

public class EvenOrOdd {
int x;

public void Sample() {
if (x % 2 == 0) {
System.out.println("\n First Method: You have entered EVEN Number");
}
else {
System.out.println("\n First Method: You have entered ODD Number");
}
}
public int SampleAgain(int Number) {
if (Number % 2 == 0) {
return 1;
}
else {
return 0;
}
}
}```

Within the Main Java Even Odd program, we will create an instance of the above-specified class and call the methods to find even or odd in Java

```package SimpleNumberPrograms;

import java.util.Scanner;

public class EvenorOddUsingClass {
private static Scanner sc;
public static void main(String[] args) {
int Number;
sc = new Scanner(System.in);
System.out.println("\nPlease Enter the integer Value to check: ");
Number = sc.nextInt();

EvenOrOdd eo = new EvenOrOdd();
eo.x = Number;

eo.Sample();
if (eo.SampleAgain(Number) != 0) {
System.out.println("\n Second Method: You have entered EVEN Number");
}
else {
System.out.println("\n Second Method: You have entered ODD Number");
}
}
}```
`````` Please Enter the integer Value to check:
45

First Method: You have entered ODD Number

Second Method: You have entered ODD Number``````

Even Odd Program in Java EvenOrOdd Class Analysis:

• First, we declared a function Sample with zero arguments. Within the function, we used the If statement to check whether the given number is perfectly divisible by two or not. If it is true, we are printing the Even Number statement otherwise, the Odd Number statement using the System.out.println statement.
• Next, we declared an integer function SampleAgain with one argument. Within the function, we used the If statement to check whether the given number is perfectly divisible by 2 or not. If it is true, we are returning Value 1 otherwise, 0.

Main Class Analysis:

In this Even Odd Program in Java, First we created an instance / created an Object of the EvenOrOdd Class

`EvenOrOdd eo = new EvenOrOdd();`

Next, we are assigning the user entered values to the EvenOrOdd Class variables.

`eo.x = Number;`

Next, we are calling the Sample method. Note, this is the first method that we created with the void keyword, and this method will check for even or odd and print the output from the Sample Class itself.

`eo.Sample();`

Next, Within the If statement, we are calling the SampleAgain method and checking whether the return value is Not Equal to Zero or Not. If the condition is True, the statement inside the If block will execute. Otherwise, it will run the statement inside the else block.