Java Program to Check Whether a Number is Even or Odd
Check Whether a number is Even or Odd using Java
Given an integer input num, the objective is to write a code to Check Whether a Number is Even or Odd in Java Language. To do so we check if the number is divisible by 2 or not, it’s Even if it’s divisible otherwise Odd.
Example Input : num = 11 Output : Odd
Check Whether a Number is Even or Odd in Java
Given an integer input the objective is to write a Java code to Check Whether a Number is Even or Odd. To do so the main idea is to divide the number by 2 and check if it’s divisible or not. It’s an Even number is it’s perfectly divisible by 2 or an Odd number otherwise.
Here are the Methods to solve the above mentioned problem,
- Method 1 : Using Brute Force
- Method 2 : Using Ternary Operator
- Method 3 : Using Bitwise Operators
Method 1 : Using Brute Force
This method simply checks if the given input integer is divisible by 2 or not. If it’s divisible then print Even or Odd otherwise.
Algorithm
The working of the above code is mentioned below
- Input an integer input “number“
- Check whether the number is divisible by 2
- This means using modulo/remainder operator leaves 0 as a remainder
- Do : if (number % 2 == 0)
- if yes, print “Even number”
- if not, print “Odd number”
Java Code
public class Main { public static void main(String[] args) { int number = 29; //checking whether the number is even or odd if (number % 2 == 0) System.out.println(number + " is Even"); else System.out.println(number + " is odd"); } }
Output
29 is Odd
Method 2 : Using Ternary Operator
This Method uses the ternary operator to check if the integer input is divisible by 2, If true print Even or Odd otherwise.
Algorithm
The working of the above code is as follows,
- Input an integer input “number“
- Check whether the number is divisible by 2 using the ternary operator
- (number % 2) ? (cout <<“Even”) : (cout << “Odd”)
Java Code
public class Main { public static void main (String[]args) { int number = 29; //Checking if the number is divisible by 2 String status = number % 2 == 0 ? " is Even" : " is Odd"; System.out.println (number + status); } }
Output
29 is Odd
Method 3 : Using Bitwise Operator
This Method uses bitwise operators to check if a given number is Even or Odd.
Algorithm
The working of the above code is as follows,
- If we have any number ‘n‘ doing bitwise ‘&‘ operation will give resultant as
- 1: If n is odd
- 0: if n is even
Java Code
public class Main { public static void main (String[]args) { int number = 29; if (isEven (number)) System.out.println ("Even"); else System.out.println ("Odd"); } // Returns true if n is even, else odd static bool isEven (int number) { // n & 1 is 1, then odd, else even return (!(number & 1)); } }
Output
Odd
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Getting Started
- ASCII Table
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Finding Prime Factors of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
Login/Signup to comment