Monday, June 10, 2013

How to find the number is even without using arithmetic Operators

public class EvenOrOddWithoutArithOprs {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);

        System.out.println("Enter any number : ");

        // return the user input as integer
        int number = in.nextInt();

        // Finding Even and Odd number using Bitwise AND operator in Java.

        System.out.printf("Finding number if its even or odd using bitwise AND operator %n");

        // For Even numbers
        // XXX0
        // 0001 AND
        // 0000
        if ((number & 1) == 0) {
            System.out.println("number %d is even number %n" + number);
        } else {
            System.out.println("number %d is odd number %n" + number);
        }
    }

}

No comments:

Post a Comment