Avocado_man
Avocado_man

Reputation: 139

Use of % modulo function

going through a tutorial I've learnt that the modulus function returns the remainder of the equation. Thus, for example, 3 % 4 equals 3

But I don't seem to understand how 25 * 3 % 4 = 3. What's happened to 25?

I've run the script on PowerShell as well as online Google calculator, returns the same. Anyone willing to explain this, kindly?

Upvotes: 2

Views: 135

Answers (1)

Bathsheba
Bathsheba

Reputation: 234635

Since * and % have exactly the same precedence, associativity comes into play. Since both operators are evaluated from left to right (i.e. the associativity is from left to right), your expression is equivalent to

(25 * 3) % 4

which is, of course, 75 % 4 which is also 3.

Upvotes: 5

Related Questions