-1

I'm doing java and I'm having trouble understanding how it's calculating this sum. 5+3/2*7-8=4
How it's getting 4?

The order in which arithmetic operators are applied in a calculation are as follows: 1. Negation, - 2. Multiplication and Division, *, /, % 3. Addition and Subtraction, +, -

Also what is negation?

Cœur
  • 37,241
  • 25
  • 195
  • 267
TomHanks
  • 1
  • 8

2 Answers2

1

In java, when you divide two integers you receive an integer. So in your equation, 3 / 2 = 1. Which becomes 5 + (1 * 7) - 8 = 4

5 + 3 / 2 * 7 - 8 = 4
5 + 1 * 7 - 8 = 4
5 + 7 - 8 = 4
5 + -1 = 4
Brian Cain
  • 946
  • 1
  • 7
  • 20
0

That's the way I understand it: - since division and multiplication are the highest priority they will be processed first, from left to right; so 3/2 gives 1, as @Brian Cain noted; then we multiply that 1 by 7 -> that gives 7 - so far we have 5 + 7 - 8, which again processed from left to right gives 4

I'm not very much into Java, but negation seems to me like an operation related to boolean type: that's a type which takes either true or false, and by using negation (!) you flip the value between these two, but please double-check it

Tomek
  • 621
  • 1
  • 6
  • 16