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

iOS

Confusion about Remainders

I understand that 7 % 3 = 1, but why does 3 % 7 = 3 & 5 % 7 = 5?

Thanks!

1 Answer

With the % operator, the result is the number you get when subtracting the highest multiple of the number you are dividing by from the number that is being divided.

// 6 is the max multiple of 3 (3(2))that is still below 7 (the next would be 9).  
3 * 2 = 6

// however it is still one less than seven, making the remainder 1. 
7-6 = 1

so for 3 % 7:

// Because 7 cannot go into 3 or 5 even once without producing something other than a whole number, 
// the multiple of 7 is 0
7 * 0 = 0 

//resulting in the remainder being the entire number that was being divided.
3 - 0 = 3

Hope that helps, I doubt thats how a mathematician would explain it, but it's how I've always thought about it.

It does. Thanks!