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 Swift Basics (retired) Operators Operator Precedence

How is 3 % 7 = 3?

I don't understand how 3/7 has a remainder 3

3 Answers

Hi Canbin,

It's because 3 / 7 = 0 ---> therefore 3 remains.

Got it, thank you so much!

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

We are talking about integer division here.

3 / 7 = 0 with 3 as remainder, it makes sense.

I see it now, thank you so much!

Jonathan Söder
Jonathan Söder
7,428 Points

I just recently learned about this myself so here goes: Integers whole numbers, like 4, 14, 123, 38, 120391023812. It can also deal with negative numbers in some languages (or all?) like -4, -14, -123, -38, -120391023812. Modulus is the remainder. So whatever remains of the division is the answer, not the result of the division itself.

I've read there are some functions in some languages for usage with doubles (like 3.5 % 2 = 1.5)

Basically if I got this right,

3 % 7 = 3
3 % 7 = 0 with remainder of 3.
3/7 = ~0,428571..... but we're content with saying 0 like above since it's integer division. You can think of it this way: 7 can't fit into 3 in any way and give us a whole number other than 0. So the remainder is 3.

If we turn it around:
7 % 3 = 1
7 / 3 = 2 with a remainder of 1.
This time, 3 can fit into 7 two times and 2 is an integer. Remainder is 1.


Another example:
2 % 4 = 2
2 % 4 = 0 with remainder of 2.
2 / 4 = 0.5. 4 can't fit into 2 and give us an integer other than 0. So the remainder is 2.

If we turn it around:
4%2 = 0
4/2 = 2 with a remainder of 0.
This time, 2 fits in 4 two times and 2 is an integer but there is no remainder. So the answer is 0.

3%7 = 3
4%7 = 4
5%7 = 5
6%7 = 6
7%7 = 0
8%7 = 1
9%7 = 2
etc

I know why 3 is the remainder now,thank you

Rob Pelgrum
Rob Pelgrum
5,854 Points

Thank you for this. Now i understand the remainder