The Artima Developer Community
Sponsored Link

Java Buzz Forum
Program Check even or odd Without using modulus and division operators

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Program Check even or odd Without using modulus and division operators Posted: Mar 2, 2015 1:43 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Program Check even or odd Without using modulus and division operators
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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.

Program to check number is even or odd by using modulus "%" operator

  1. package instanceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class EvenorOdd {
  5.  
  6. public static void main(String []args )    {
  7.  
  8.     int number;
  9.     Scanner in= new Scanner(System.in);
  10.  
  11.     System.out.println("Enter a number to check even or odd");
  12.     number=in.nextInt();
  13.  
  14.     if((number % 2)==0){
  15.         System.out.println(+number+" is Even number");
  16.     }else{
  17.         System.out.println(+number+" is Odd Number");
  18.     }
  19.  
  20. }
  21. }

  1. Enter a number to check even or odd
  2. 37
  3. 37 is Odd Number


Program to check number is even or odd by using division "/" operator

  1. package instanceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class EvenorOdd {
  5.  
  6. public static void main(String []args )    {
  7.  
  8.     int number;
  9.     Scanner in= new Scanner(System.in);
  10.  
  11.     System.out.println("Enter a number to check even or odd");
  12.     number=in.nextInt();
  13.  
  14.     if((number / 2)*2=number){
  15.         System.out.println(+number+" is Even number");
  16.     }else{
  17.         System.out.println(+number+" is Odd Number");
  18.     }
  19.  
  20. }
  21. }

  1. Enter a number to check even or odd
  2. 46
  3. 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 and "%" operator

    1. package instanceofjava;
    2. import java.util.Scanner;
    3.  
    4. public class EvenorOdd {
    5.  
    6. public static void main(String []args )    {
    7.  
    8.     int number;
    9.     Scanner in= new Scanner(System.in);
    10.  
    11.     System.out.println("Enter a number to check even or odd");
    12.     number=in.nextInt();
    13.  
    14.     if((number & 1)==0){
    15.         System.out.println(+number+" is Even number");
    16.     }else{
    17.         System.out.println(+number+" is Odd Number");
    18.     }
    19.  
    20. }
    21. }

    1. Enter a number to check even or odd
    2. 9
    3. 9 is Odd Number

    Read: Program Check even or odd Without using modulus and division operators

    Topic: Java : Collection : TreeMap (Ascending and Descending Order Sample Program) - Playlist Previous Topic   Next Topic Topic: Java : Collection Framework : TreeMap (Comparator)

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use