# Java Odd Even Program

Write a Java Odd Even Program to show you, How to check whether the given number is Odd or Even number using If Statement and Conditional Operator with example. If the number is divisible by 2, it as 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 Odd Even Program using IF Condition

This Java 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.

```//Java Odd Even Program using If Else 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

## Java Odd Even Program using Conditional Operator

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

```//Java Odd Even Program using Conditional Operator
package SimpleNumberPrograms;

import java.util.Scanner;

public class EvenorOddUsingConditional {
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 Odd Even 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 Odd Even 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``````

## Java Odd Even Program 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 EvenorOddUsingMethods {

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();

evenOrOdd(Number);

}
public static void evenOrOdd(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 evenOrOdd method

`evenOrOdd(Number);`

Let us see the code inside the evenOrOdd method

```public static void evenOrOdd(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");
}
}```

From the above code snippet, 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 Odd Even 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 evenOrOdd() {
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 evenOrOddAgain(int Number) {
if (Number % 2 == 0) {
return 1;
}
else {
return 0;
}
}
}```

Within the Main Java Odd Even 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 Even or Odd: ");
Number = sc.nextInt();

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

eo.evenOrOdd();
if (eo.evenOrOddAgain(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 Even or Odd:
45

First Method: You have entered ODD Number

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

Java Odd Even Program EvenOrOdd Class Analysis:

• First, we declared a function evenOrOdd 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 Even Number statement otherwise, Odd Number statement using System.out.println statement.
• Next, we declared an integer function evenOrOddAgain 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 Java Odd Even Program, 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 evenOrOdd 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 EvenOrOdd Class itself.

`eo.evenOrOdd();`

Next, Within the If statement, we are calling the evenOrOddAgain 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.