Java program to find Even or Odd Number

In Java, we can find if a number is even or odd using either an if-else condition or a ternary operator. Numbers that are divisible by 2 are called even numbers, while those that are not divisible by 2 are called odd numbers.

For example, if the input number is 2, the output will be “2 is an even number.” In this article, we will learn through examples, detailed logic, and a program explanation for better understanding.

Required Knowledge

Problem Statement

We need to write a Java program to check if a number is even or odd. The program should take an integer as input and print whether it is an even or odd number. This above action should be done in our program. Let’s look at an example,

Example 1:

For given input number 20, Output should be “Even number”.

Explanation:

Input number: 20

Remainder: 20%2=0 (20 is divisible by 2)

Hence, 20 is a Even number

Example 2:

For a given input number 15, output should be “Odd number”.

Explanation:

Input number: 15

Remainder: 15%2= 1(Not divisible by 2)

Hence, 15 is a Odd number

Logic to Find Even and odd number

There are multiple logics to check a given input number is even or odd number in Java Programing language.

Logic 1: Using if-else

  • Here we are checking the remainder of the given input number.
  • When a number is divided with 2, It will give the remainder as Zero. Then condition is satisfied and prints “Number is Even.
  • If remainder is not Zero, else block will execute and print “Number is Odd Number”.

Logic 2: Ternary Operator

  • This operator takes three operands and works similar to the if else statements. If condition true, it prints first expression and if condition becomes false then it prints second expression.
  • When a number is divided with 2 and get the remainder as Zero, then it is Even number otherwise it is Odd Number.

Java Program to check even or odd number

Program 1: Using if-else

In the below program to use if else statement to check even or odd number in java.

import java.util.Scanner;
public class EvenOrOdd {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("enter the number");
		int number = s.nextInt();
		if (number % 2 == 0)
			System.out.println("Number is even");
		else
			System.out.println("Number is odd");
		s.close();
	}
}

Output:

enter the number
24
Number is even

Program Explanation to print Even or Odd Number

  • The java program imports the Scanner class to facilitate user input.
  • Defines a class named EvenOrOdd.
  • In the main method:
    • Creates a Scanner object ‘s’ to read user input.
    • Asks the user to enter a number.
    • Reads the integer input and stores it in the variable ‘number’.
    • Checks if the number is divisible by 2 without leaving a remainder.
    • If the condition is true, it prints “Number is even”.
    • If the condition is false, it prints “Number is odd”.
    • Closes the Scanner object to release system resources.

Program 2: Using Ternary Operator

In the below program we are using Ternary Operator to find even or odd number in Java.

import java.util.Scanner;
public class EvenOrOdd {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("enter the number");
		int number = s.nextInt();
		String result;
		result=(number%2==0)?
		       number+" is even":
		       number+" is  odd";
		System.out.println(result);
		s.close();
	}
}

Output:

enter the number
13
13 is odd

Program Explanation to print Even or Odd Number

  • The java program imports the Scanner class to facilitate user input.
  • Defines a class named EvenOrOdd.
  • In the main method:
    • Creates a Scanner object ‘s’ to read user input.
    • Asks the user to enter a number.
    • Reads the integer input and stores it in the variable ‘number’.
    • Uses a ternary operator to assign the appropriate message to the variable ‘result’ based on whether the number is even or odd.
    • Prints the ‘result’ message.
    • Closes the Scanner object to release system resources.

Conclusion

In this article, we explored how find if a number is even or odd number using if-else statements and the ternary operator. We understood the logic behind this and moved on to writing the program.

For more information on related topics, you might find these articles helpful:

  • Understanding the concept of Conditional statements in Java
  • Concepts of Operators in Java
  • Top 10 Java Programming tips

We hope you find this article helpful. If you have any doubts or need any further clarification, feel free to leave a comment below. Don’t forget to subscribe our newsletter to get updates on Java Programming tips and tutorials.

Stay focused and Happy learning!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.