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 Math

I don't get how modulus works. In the example he says the modulus is one, but I don't see how he got that.

That's all

3 Answers

Andrew Trachtman
Andrew Trachtman
3,680 Points

This confused me as well. The trick here is that everything is performed from left to right (It threw me off seeing 21%3 not return 0, but then I realized I was getting ahead of myself).

Let me break it down for you.

5 + 2 - ( (3*7) / 3 ) % 3

So we get that 3*7 = 21
5 + 2 - ( 21 / 3 ) % 3

21/3 = 7
5 + 2 - (7) % 3

7/3 = 2 with a remainder of 1. So 7%3 = 1

5 + 2 - 1

Final result:
7 - 1 = 6

I hope this helps clarify things.

Thank you very much! I think he just left out some steps in instruction and that confused me.

Ramses Martinez
Ramses Martinez
2,011 Points

You can also see the model this way...

5 % 3

How many times does the 3 fit the 5?... 1, So

3 x 1 = 3

Now we search the difference (Residue)

5 - 3 = 2

therefore

5 % 3 = 2

Thanks! He didn't really explain it and since I had never done that operation before, I wasn't sure what I was suppose to be looking for.

I was also very confused by the % operator, as I've never used it before. I would recommend TreeHouse might want to explain this operator a little more clearly. Ramses, your "How many times does x fit into y...?" really helped me out.

Jon Wood
Jon Wood
9,884 Points

Modulus is doing a division on the two numbers and it returns the remainder.