Java Even Odd Program

Write a Java Program to show you How to check whether the given number is an 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.

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

Java Even Odd Program using IF Condition

This program allows the user to enter any integer value. Then, this Java program checks whether that number is even or odd using the 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 example program, 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.

if (Number % 2 == 0) {

As we all know, the number divisible by 2 is an 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 an Even number.
  • If the condition is False, the Java Programming Else block statement will print. That is an Odd number.

We entered the value as 48 to check, and the program returns the even number as output.

Java Odd Even Program using If

Java Even Odd Program using Conditional Operator

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

// using Conditional Operator
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 screenshot below, you can observe that we entered 19 as input. As you can see, it is returning Odd Number as Output.

Java Even Odd Program using Conditional Operator

You can observe from the above code that we used the 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 and Even Program using Functions

In this 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 it in a separate method.

// using Methods
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 Program using Functions

In this Program, If you observe the following statement, we call 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 perform the same operation explained in our previous example.

Java Even Odd Program using OOPs

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

// using Class
package SimpleNumberPrograms;

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

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

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");
 }
 else {
 System.out.println("\n Second Method: You have entered ODD");
 }
 }
}
 Please Enter the integer Value to check: 
45

 First Method: You have entered ODD

 Second Method: You have entered ODD

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 true, we are returning Value 1; otherwise, 0.

Main Class Analysis:

In this Program, First, we created an instance / created an Object of the EvenOrOdd Class

EvenOrOdd eo = new EvenOrOdd();

Next, we assign the user-entered values to the EvenOrOdd Class variables.

eo.x = Number;

Next, we are calling the Sample method. Note that this is the first method we created with the void keyword, which 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.

Please refer to

  1. First 10 Even Natural Numbers
  2. First 10 Odd Natural Numbers
  3. Print Odd Numbers from 1 to N
  4. Print Even Numbers from 1 to N
  5. The Sum of Even and Odd
  6. The Sum of Even
  7. The Sum of Odd