How does modulo works when first number is smaller than the second? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How does modulo works when first number is smaller than the second?

I tried 2%5 in Python and it returns 2. Even 2%4 and 2%6 returns the same 2 as result. Why is that so?

8th Mar 2018, 2:20 PM
Sindhu
Sindhu - avatar
1 Answer
+ 3
You know that the modulo returns the remainder. So, what's happening is that the smaller number is not divisible by the larger, so the smaller number IS the remainder. "5 goes into 2 zero times with 2 still remaining"
8th Mar 2018, 2:34 PM
Zeke Williams
Zeke Williams - avatar