# Order of Operations

First-time user going through the Java beginners training, and I am having difficulty determining the arithmetic order of this equation “int expression3 = 5 * 4 % 3 - 2 + 1;”. The system is saying 1 is the correct answer. And trust me, math has never been my thing, but for the life of me, I can’t figure out how that number is correct.

I correctly calculated the 1st two as “-6” and “3” respectfully, but I could never get 1 on the expression3:

public class Operations {
public static void main(String args) {

``````int expression1 = 5 % 2 - (4 * 2 - 1);
System.out.println(expression1);

int expression2 = (3 + (2 * 2 - 5)) + 6 - 5;
System.out.println(expression2);

int expression3 = 5 * 4 % 3 - 2 + 1;
System.out.println(expression3);

}
``````

}

Multiplication and modulo share the same precedence, so you evaluate from left to right in this expression.

``````5 * 4 % 3 - 2 + 1 // 5 * 4 first
20 % 3 - 2 + 1 // 20 % 3 next
2 - 2 + 1 // pretty easy from here
1
``````
1 Like

Hi Midlinder,

Excellent! I greatly appreciate the assistance on this one. I see my error! I had tried this order, but I wasn’t using the “remainder” of the modulo in my calculation, but rather the actual whole number of the division. So while I was fighting the order, it was always with the wrong result of the modulo. Got it!

Go it!

Thanks again!

1 Like