0
If(num % 2==0); why the percent sign is used ?
7 Antworten
+ 2
It is used to find the remainder of the number after dividing by 2 through which u can find if the given number is odd or even
+ 5
Rehan Glasswala
% is known as modulus operator which is used to get reminder value.
So if num is 10 then 10 % 2 will be 0
if num is 11 then 11 % 2 will be 1
+ 2
Rehan Glasswala to give you another angle, the modulus defines how many numbers are in the base. Modulus 7 has digits 0...6; modulus 25 has 0...24.
In modulus it counts from 0 up to the last number in the modulus and then wraps back to zero and repeats. Just like in a clock the second hand wraps back to 0 every minute. Seconds are modulus 60.
What is modulus 2? Binary counting, of course!
num%2 is counting up to num but its lowest bit always holds either 0 or 1. 0 is every even number, 1 is every odd number. Since num is already stored in binary there is a fast shortcut to the answer by looking at num's lowest bit.
if(num&1==0) //far superior to %2 in speed
This way does no division operation or determining of remainder. The answer is already stored in the number itself!
+ 2
Thanks
+ 1
5%2=1#modulo(not whole part after division)
5//2=2 #floor division
5/2 =2.5 #ordinary division
+ 1
Thank you so much now I understood 😊👍
+ 1
Thanks