Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Check Whether a Number is Even or Odd
In this article we will see different ways to check whether a number is even or odd in Java. So, before going to the actual concept let’s first understand what is this even number and odd number then we will move to next part.
Even Number :
If a number is divisible by 2 and generates remainder of 0 then that number is called as even number.
For example:
14 is an even number because 14%2 returns remainder 0. 80 is an even number because 80%2 returns remainder 0.
Odd Number :
If a number isĀ not divisible by 2 and does not generate remainder of 0 then that number is called as odd number.
For example:
33 is an odd number because 33%2 returns remainder 1. 17 is an odd number because 17%2 returns remainder 1.
Different ways to check whether a number is even or odd:
- Using modulo(%) operator
- Using division(/) operator
- Using ternary operator
- Using Bit wise AND(&) operator
- Using Bit wise OR(|) operator
Method-1 : Using modulo(%) operator
By using the modulo operator also we can check a number is even or odd.
Approach:
- Divide the number by 2 modulo operator.
- If the remainder is 0 then it is an even number and if the remainder is not equal to 0 then it is odd.
Let’s see the below program to understand how it actually works.
import java.util.*; class Check { public static void main(String args[]) { // Scanner class object created for input Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //Take a number input from user int num=sc.nextInt(); //get the remainder using modulo operator int rem=num%2; // If the remainder is equals to 0 then it is even number // else it is odd number if(rem==0) { System.out.println("Even number"); } else { System.out.println("Odd number"); } } }
Output : Enter a number: 90 Even number //Another case Enter a number: 9 Odd number
Method-2 : Using division(/) operator
By using the division operator also we can check a number is even or odd.
Approach :
- Divide the number by division operator.
- Then Again multiply 2 with quotient.
- If quotient multiplied with 2 is equal to original number(dividend) then it is even.
- If quotient multiplied with 2 is not equal to original number(dividend) then it is odd.
For example:
Example-1 Original number(dividend)=44 Then 44/2==22(quotient) Then 22*2=44(original number) So, it is even
Example-2 Original number(dividend)=33 Then 33/2==1(quotient) Then 1*2=2(Not original number) So, it is odd
import java.util.*; class Check { public static void main(String args[]) { // Scanner class object created for input Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //Take a number input from user int num=sc.nextInt(); //using division operartor if((num/2) * 2==num) { System.out.println("Even number"); } else { System.out.println("Odd number"); } } }
Output: Enter a number: 44 Even number // Another case Enter a number: 33 Odd number
Method-3 : Using ternary operator
By using the ternary operator also we can check a number is even or odd.
Synatx: variable = Expression1 ? Expression2: Expression3
Let’s see the below program to understand how it actually works.
import java.util.*; class Check { public static void main(String args[]) { // Scanner class object created for input Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //Take a number input from user int num=sc.nextInt(); // using ternary operator String res=(num%2==0) ? "Even number":"Odd number"; System.out.println(res); } }
Output: Enter a number: 44 Even number // Another case Enter a number: 33 Odd number
Method-4 : Using Bit wise AND(&) operator
By using the bit wise AND(&) operator also we can check a number is even or odd.
Approach :
- Perform the bit wise AND operation of the number with 1.
- If the result is 1 then it is odd.
- And if the result is 0 then it is even.
Let’s see the below program to understand how it actually works.
import java.util.*; class Check { public static void main(String args[]) { // Scanner class object created for input Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //Take a number input from user int num=sc.nextInt(); // performing bit wise AND operation if ((num & 1) == 1) { System.out.println("Odd number"); } else { System.out.println("Even number"); } } }
Output: Enter a number: 44 Even number // Another case Enter a number: 33 Odd number
Method-5 : Using Bit wise OR(|) operator
By using the bit wise OR(&) operator also we can check a number is even or odd.
Approach :
- Perform the bit wise OR operation of the number with 1.
- If the value of the number incremented by 1 then it is even.
- And if the value remains unchanged then it is odd.
Let’s see the below program to understand how it actually works.
import java.util.*; class Check { public static void main(String args[]) { // Scanner class object created for input Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //Take a number input from user int num=sc.nextInt(); // performing bit wise OR operation if ((num | 1) > num) { System.out.println("Even number"); } else { System.out.println("Odd number"); } } }
Output: Enter a number: 44 Even number // Another case Enter a number: 33 Odd number
Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.
Related Java Basic Programs:
- Java Program to Print an Integer (Entered by the User)
- Java Program to Add Two Integers
- Java Program to Multiply two Floating Point Numbers
- Java Program to Find ASCII Value of a character
- Java Program to Compute Quotient and Remainder
- Java Program to Swap Two Numbers
- Java Program to Find the Largest Among Three Numbers
- Java Program to Find the Frequency of Character in a String
- Java Program to Remove All Whitespaces from a String
- Java Program to Round a Number to n Decimal Places
- Java Program to Check if a String is Empty or Null