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 trial

C# C# Basics (Retired) Perform Order of Operations

I'm stuck on the maths for C# basics the question is 8 * (7+8) % 5 + 6/2

I thought you would do the parenthesis first leaving 15 between them, multiplied by 8 giving the value of 120, then taking 5 away you are left with 115, then dividing 6 by 2 is 3, adding 115 and 3 together gets 118 but it is wrong. I'm not sure how I'm messing up the order but any help would be appreciated

If you want this solved for you the lazy way, you can try typing in the equation into the REPL by typing csharp in the console with workspaces May have already known this but i guess it may help haha

Arturs Dubra
Arturs Dubra
4,748 Points

I did exactly like Keanu suggests, it is fast and easy way how to handle this problem.

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

It's not subtracting by 5 .,.. it's getting the mod of 120 and 5. So it looks like this.

8 x (7 + 8) % 5 + 6 /2 =

8 x 15 % 5 + 6/2 =

120 % 5 + 6/2 =

0 + 6 / 2 = because 120 divided by 5 has a remainder of 0 aka it's evenly divisible by 5

0 + 3 =

Final answer: 3

Happy coding! :smiley:

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

Here's a trick for the modulo!

7 + 8 is 15, which is divisible by 5. Because of this, multiplying that number by any other integer would still create a number divisible by 5. Because 8 * (7 + 8) is going to be divisible by 5, modding that number by 5 can only result in 0. That means that 8 * (7 + 8) % 5 is going to evaluate to 0, so you only need to worry about 6/2.

hey thanks you two are live savers ahaha