3

I recently enrolled in a Java class and I have a question regarding modulus division.

I get an example in my textbook:

( 100 - 25 * 3 % 4 ) = 97

How does this equal 97? I’ve tried every single possiblity and I just can’t seem to figure it out.

Can someone please be so kind to break it down for me.

Thanks in advance.

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
DaBruh
  • 41
  • 3

2 Answers2

9

Operator Precedence

( 100 - ((25 * 3) % 4) ) = 97

25*3=75

75 MODULO 4=3

100-3=97

That's it.

When you're unsure about operators' priority put parentheses everywhere.

Community
  • 1
  • 1
StephaneM
  • 4,779
  • 1
  • 16
  • 33
  • 1
    So that would be (100 - ((25 * 3) % 4)) = 97 with parentheses? – Paul Brindley Sep 25 '14 at 10:55
  • 3
    Yes, look at this page: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html * and % have the same precedence, they are evaluated from left to right, first the * then the % and finally the -. – StephaneM Sep 25 '14 at 10:59
  • Thanks Stephan for your answer! Appreciate it! – DaBruh Sep 25 '14 at 12:12
1

The operators *, /, and % are called the multiplicative operators. They have the same precedence and are syntactically left-associative (they group left-to-right).

So whenever there is A op1 B op2 C and both op1 and op2 are *, / or % it's equivalent to

(A op1 B) op2 C

  25 * 3 % 4 -> a * b % c 
  (25*3)%4->(a*b)%c

(-) operator is right to left.so - operator performs the calculation after right side expression completed. so the answer is 100-(25*3)%4=100-3=97

Selva
  • 1,620
  • 3
  • 33
  • 63