0

# If(num % 2==0); why the percent sign is used ?

7th Jan 2022, 6:36 PM
Rehan Glasswala
+ 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
9th Jan 2022, 10:06 AM
K RAMALAKSHMI + 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
7th Jan 2022, 6:43 PM
A͢J + 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!
7th Jan 2022, 8:25 PM
Brian + 2
Thanks
8th Jan 2022, 11:22 AM
Rehan Glasswala
+ 1
5%2=1#modulo(not whole part after division) 5//2=2 #floor division 5/2 =2.5 #ordinary division
7th Jan 2022, 6:45 PM 