New course! Every coder should learn Generative AI!
+ 1

# Modulus function in programming

anyone can explain why 2%100=2 not 0? and 18%2=0.....?

18th Jan 2019, 12:43 AM
Anjum 007
+ 3
At first, you need to understand what modulus does: Take any integer N and any integer M Now take N % M The modulus simply takes as many times M from N as possible like this: If(M <= N) N = N - M So if M = 2 and N = 18 we get this: [ a <= b ] means a is smaller than or equal to b 18 % 2 = (2 <= 18) -> 18 - 2 = 18 (2 <= 16) -> 16 - 2 = 14 (2 <= 14) -> 14 - 2 = 12 (2 <= 12) -> 12 - 2 = 10 (2 <= 10) -> 10 - 2 = 8 (2 <= 8) -> 8 - 2 = 6 (2 <= 6) -> 6 - 2 = 4 (2 <= 4) -> 4 - 2 = 2 (2 <= 2) -> 2 - 2 = 0 (2 <= 0) is false so it will return 0 (100 <= 2) is false, so it will return 2
18th Jan 2019, 12:53 AM
Roel
+ 4
This is a simple divide which you learn in 2nd class.% it means remainder.so how we divide in when we are in 2nd standard. Like this. ------- 100 | 2 |0 _______ 0 So in this divide remainder is 0. I know you are thinking now but it's answer should be change if we take value in decimal.but remember that when you want to find the remainder with the help of % then python divide like when we do divide and find remainder in our 2nd class or you can say in our childhood.
18th Jan 2019, 1:10 AM
Maninder \$ingh
0
thanx Roel 😍 i really understand this now and you too Maninder \$ingh 💕
19th Jan 2019, 12:14 AM
Anjum 007
Hot today
Python