Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAbdalgader Mohamed
672 Points8*(7+8)%5+6/2=
solve of this equetion
4 Answers
Steven Parker
231,210 PointsWhat's important here is operator precedence. Jonathan mentioned the mnemonic phrase often used to indicate the order, but he forgot to apply it himself. The P ("Please") stands for parentheses, which always must be performed first.
Adrian added extra parentheses to indicate the evaluation order. This is sometimes done in actual code to make the evaluation easier to understand later (or sometimes for safety when the coder forgets the precedence rules!).
Alan got the right answer even though the steps were not quite right. The actual steps are:
- [P (and A)] (7 + 8) = 15
- [M] 8 * 15 = 120
- [D] 120 % 5 = 0 (% has the same precedence as /)
- [D] 6 / 2 = 3
- [A] 0 + 3 = 3
M (multiply) and D (divide) actually have the same precedence, but operators of the same precedence are performed left-to-right, so in this case, step 2 is the multiply.
Adrian Clare
7,853 PointsHi,
The answer is 3. ((8* (7+8)) %5) + (6/2) = 3
Alan Ayoub
40,294 Pointsok, follow me here.
8*(7+8)%5+6/2=
Step 1: (7+ 8) = 15
Step 2: 15 % 5 = 0
So now we have:
8 * 0 +6/2=
Step 3: 8 * 0 = 0
Step 4: 0 + 6/2 = 3
Answer = 3
johnathanmyers
19,994 PointsThe answer is 62 If you're having trouble try copying the equation and paste it into repl remember to always keep in mind int and double... And remember "Please Excuse My Dear Aunt Sally" for the order of operations.... ;)
(Edit: See Steven Parker's answer... 62 is the first question's answer... Parentheses make all the difference!)
johnathanmyers
19,994 Pointsjohnathanmyers
19,994 PointsAh, yes. you are correct Steven. I clicked the view quiz button and based my answer off the first question which was 8 * 7 + 8 % 5 + 6 / 2 I should have paid closer attention.
Alan Ayoub
40,294 PointsAlan Ayoub
40,294 PointsNice, excellent explanation.