- Checking the number even or odd program is very easy. Anybody can solve this but there is a condition we need to see.
- When we get this question in interview "write a program to check given number is even or odd" always continues with "without using modulus and division operators".
- Before going to actual program lets see how to check a number is even or odd by using modulus and division operators.
Program to check number is even or odd by using modulus "%" operator
- package instanceofjava;
- import java.util.Scanner;
- public class EvenorOdd {
- public static void main(String []args ) {
- int number;
- Scanner in= new Scanner(System.in);
- System.out.println("Enter a number to check even or odd");
- number=in.nextInt();
- if((number % 2)==0){
- System.out.println(+number+" is Even number");
- }else{
- System.out.println(+number+" is Odd Number");
- }
- }
- }
Output:
- Enter a number to check even or odd
- 37
- 37 is Odd Number
Program to check number is even or odd by using division "/" operator
- package instanceofjava;
- import java.util.Scanner;
- public class EvenorOdd {
- public static void main(String []args ) {
- int number;
- Scanner in= new Scanner(System.in);
- System.out.println("Enter a number to check even or odd");
- number=in.nextInt();
- if((number / 2)*2==number){
- System.out.println(+number+" is Even number");
- }else{
- System.out.println(+number+" is Odd Number");
- }
- }
- }
Output:
- Enter a number to check even or odd
- 46
- 46 is Even Number
Without using modulus and division operators:
- The above two programs will check number is even or odd and displays result.
- Now we need to write a program to check even or odd without using modulus and division operators.
- It is very simple if you know about operators including "BIT WISE".
- Yes using Bit Wise AND "&" operator we can check a number is even or odd.
- Before starting our program lets see how this bit wise AND "&" operator will work.
Bitwise Operators :
- Bit wise operators will work on bits at a time.
- AND : 1 & 1=1
- OR : 0 | 1= 1 , 1 | 0=1 , 1| 1= 1
- XOR: 0 ^ 1= 1 , 1^ 0=1
- NOT : !0=1
- Take two number 2 and 3
- 010 : 2
011 : 3
------
010 : 2 - ------
- Take two numbers 2 and 1
- 010 :2
001 :1
-----
000 :0
-----
- From above example we can say that on every even number & 1 gives 0.
- So this is our logic to be implemented in our program if "Number & 1==0" then its even number.
Program to check number is even or odd by using "&" operator
- package instanceofjava;
- import java.util.Scanner;
- public class EvenorOdd {
- public static void main(String []args ) {
- int number;
- Scanner in= new Scanner(System.in);
- System.out.println("Enter a number to check even or odd");
- number=in.nextInt();
- if((number & 1)==0){
- System.out.println(+number+" is Even number");
- }else{
- System.out.println(+number+" is Odd Number");
- }
- }
- }
Output:
- Enter a number to check even or odd
- 9
- 9 is Odd Number
Using Shift Operators:
- We can check even or odd by using shift operators also may be it is not a better solution but trying to cover alternative.
- Lets see how shift operators will work
- Lets do this 2 >> 1
- 2 means 010 so now we need to shift right side by 1 place
- 010
-----
001 - Here we added one "0" to the left side to shift right
- So the value became 1
- Now will try left shift 001 >> 1
001
----
010 - 010<<1=001(1) and 001 >> 001=010(2)
- By this we can say if a (number >>1)<<1 gives same then that is even number. result=input
- Lets check for odd number 3
- 011>>1=001 which is 1 and 001<<1=010 which is 2 so (3>>1)>>1=2 so result!=input
Program to check number is even or odd by using Shift operator
- package instanceofjava;
- import java.util.Scanner;
- public class EvenorOdd {
- public static void main(String []args ) {
- int number;
- Scanner in= new Scanner(System.in);
- System.out.println("Enter a number to check even or odd");
- number=in.nextInt();
- if( ( number >> 1) <<1==number){
- System.out.println(+number+" is Even number");
- }else{
- System.out.println(+number+" is Odd Number");
- }
- }
- }
Output:
- Enter a number to check even or odd
- 64
- 64 is Even Number
- package instanceofjava;
- import java.util.Scanner;
- class EvenOrOddDemo
- {
- public static void main(String[] args)
- {
- Scanner sc=new Scanner(System.in);
- System.out.println("Enter a number to check whether it is even or odd without using Modulus
- and Division: ");
- int n=Integer.parseInt(sc.next());
- float f=(float)n/(float)2;
- int d=(int)f*2;
- if(d==n)
- System.out.println(n+" is a even number");
- else
- System.out.println(n+" is a odd number");
- }
- }
Output:
- Enter a number to check whether it is even or odd without using Modulus and Division
- 4
- 4 is Even Number
You might like:
- Format dates in Java
- String Programming Interview Questions
- String vs Stringbuffer vs Stringbuilder
- Marker Interfaces
- Var Args in java
- JVM Architecture
- Java 7 Features
- Throw vs Throws
- Java 8 Features
good explanation
ReplyDeleteVery good with various method and explanations
ReplyDeleteOh my god I have to review those operations very quickly :D
ReplyDeleteimport java.util.Scanner;
ReplyDeleteclass OddEven
{
public static void main(String args[])
{
int no;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number : ");
no=s.nextInt();
if(no>0)
while(no!=0)
{
if(no==1)
{
System.out.println("Number is odd.");
return;
}
no-=2;
}
else
while(no!=0)
{
if(no==1)
{
System.out.println("Number is odd.");
return;
}
no+=2;
}
System.out.println("Number is even.");
// or
/*while(no!=0)
{
if(no==1)
{
System.out.println("Number is odd.");
return;
}
no+=2;
}
System.out.println("Number is even.");*/
}
}
Awsssmmmmmm......!!
ReplyDeleteI have one more solution.
ReplyDeleteimport java.util.Scanner;
class OddEven
{
public static void main(String args[])
{
int no;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number : ");
no=s.nextInt();
if(no>0)
while(no!=0)
{
if(no==1)
{
System.out.println("Number is odd.");
return;
}
no-=2;
}
else
while(no!=0)
{
if(no==1)
{
System.out.println("Number is odd.");
return;
}
no+=2;
}
System.out.println("Number is even.");
// or
/*while(no!=0)
{
if(no==1)
{
System.out.println("Number is odd.");
return;
}
no+=2;
}
System.out.println("Number is even.");*/
}
}
this article very helpfull
ReplyDeleteXOR is ^ in JAVA
ReplyDeletegood experiences
ReplyDelete