Java Program to Check Whether a Number is an Even or Odd

Hello Shouters !! Today we will make a Java Program to Check Whether a Number is an Even or Odd.
What is an Even Or Odd Number?
The number is said to be even if the number is completely divisible by the number 2 which means after division it gives a remainder of 0. An odd number is a number that leaves remainder 1 after division with 2.
1. First Method- Division Method
In this method, we will divide the number by 2 and check if the remainder is 1 or not. Depending on the value we will check the number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Division Method - Java Program to Check a Number is an Odd/Even Number import java.util.*; class EvenOdd{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the Number you want to Check"); int number = sc.nextInt(); if (number% 2 == 0) System.out.println("Even Number"); else System.out.println("Odd Number"); } } } |
Output – Enter the Number you want to check 24 Even Number
2. Second Method- Using Bitwise Operator
In this method, we will Bitwise And (&) the number with 1 and we will get one only if the number is odd.
Example- The binary code of 9 is 1001 and the binary code of 1 is 1000 we will get 1000 as the output which makes 1 in decimal number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Division Method - Java Program to Check a Number is an Odd/Even Number import java.util.*; class EvenOdd{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the Number you want to Check"); int number = sc.nextInt(); if (number&1 == 0) System.out.println("Even Number"); else System.out.println("Odd Number"); } } } |
Output – Enter the Number you want to check 23 Odd Number
So, it is very important to understand the Java Program to Check Whether a number is odd or even.
We daily add Multiple Choice Questions for placement preparation questions and technical interview preparation on the Instagram page. Check the Instagram account: https://www.instagram.com/shoutcoders/
Frequently Asked Questions –
In mathematics, a number is said to be even if the number is completely divisible by the number 2 which means after division giving a remainder of 0. An odd number is a number that leaves remainder 1 after division with 2.
There are various methods for checking, the most used method is the division method.
Using Bitwise And operator, we can Check an odd or even number.
Yes, 0 is an even number because it satisfies all the conditions of an even number.
Yes, it is asked in many big companies like Microsoft, Amazon and many other product-based companies.