Hello can someone show me how to write or have java print for me the even numbers ? | Sololearn: Learn to code for FREE!

+4

# Hello can someone show me how to write or have java print for me the even numbers ?

10/14/2021 2:38:14 AM

Makseem

+6

AJ, 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

+6

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

+3

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

+3

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 + " ");   }   }   }   }

+3

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 + " "); }

+3

Martin Taylor ok bro and thanks for the advice.

+2

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

+2

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 =)

+2

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

+1

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

+1

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.

0

Will you please show me this in a java ?

0

I am very new i am trying to learn thank you