13 Answers

New AnswerAJ, the modulus operator uses division which is computationally more expensive than a bitwise operation, typically by a factor of around 32. An alternative technique using bitwise operations is to use... if(!(number & 1)) // even

Integer values are stored as signed 32-bit integers in Java the binary representation of an odd integer will always have the least significant bit set. I will use 8 bits to demonstrate... 00000001 = 1 00000010 = 2 00000011 = 3 00000100 = 4 00000101 = 5 even numbers end with 0 odd numbers end with 1 The bitwise and operation will return a 1 only where both bits are 1. So 5 & 1 is: 00000101 & 00000001 -------------- 00000001 Any non zero value evaluates to true, so if(number & 1) // true if number is odd since you require true if the number is even we invert the expresson to !(number &1). We could have also used... if(~number & 1) // true if even in this case we invert each bit of the number first then test the last bit. Note that it's a tilde character in front of number not a minus sign. It might be difficult to see on a mobile device. Using a bitwise and with 1 effectively masks out every bit except the last bit; which is the only bit we are interested in. See: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

Makseem The number divisible by 2 is even number. So do this use modulus operator (%) if(number % 2 == 0) //even

Display Even Numbers Example java public class DisplayEvenNumbersExample1 { public static void main(String args[]) { int number=100; System.out.print("List of even numbers from 1 to "+number+": "); for (int i=1; i<=number; i++) { //logic to check if the number is even or not //if i%2 is equal to zero, the number is even if (i%2==0) { System.out.print(i + " "); } } } }

Arun Jamson, while your solution works it is extremely inefficient. Firstly because you are using the modulus operator and secondly because there is no need to increment the loop by 1. Start the loop at 2, the first even number, and increment the loop by 2 to advance to the next even number. This will only print even numbers and require no checking. for(int i = 2; i <= number; i += 2) { System.out.print(i + " "); }

Martin Taylor can you explain it plz because I can't understand how it works(the bitwise operation solution). Tnx

Martin Taylor wow I understand this, but I never cared about the properties of the bit notation of odd and even numbers. This is surprising. From now, for even or odd number checking I'm using your method =)

Arun Jamson when i clicked run it gave an error why and also what the letter. i stands for

Martin Taylor why did you put ! And where did the Parentheses come from? I want to understand that will you show me please or help me

Makseem In the following example, I have declared a variable named number and initialized it with 100 (the limit to print the even number). We have used a for loop that executes up to 100 times and for each iteration of i the if statement checks the number is even or not. After printing each even number, the value if i is increased by 1.