# Unit 7 Lesson 2 - MOD Operator

**URL:** <https://forum.code.org/t/unit-7-lesson-2-mod-operator/34301>\
**Category:** CS Principles\
**Tags:** csp\
**Created:** [February 25, 2021, 5:35pm UTC](https://forum.code.org/t/unit-7-lesson-2-mod-operator/34301 "2021-02-25T17:35:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![msmith](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@msmith](https://forum.code.org/u/msmith)\
**Post date:** [February 25, 2021, 5:35pm UTC](https://forum.code.org/t/unit-7-lesson-2-mod-operator/34301/1 "2021-02-25T17:35:02Z")

</div>

I am having trouble understanding the MOD Operator %. When I look up videos, the descriptions do **not** match what is BRIEFLY said in Unit 7 Lesson 2 Level 2; example:  
// the MOD operator “%” - divides two numbers and returns the remainder  
// in this case - if a number divided by two has a remainder of zero, it’s an even number

A. (5%2) according to [code.org](http://code.org) is 2 with a remainder of 1  
B. If we divide 5 by 2, the result is 2.5 (so I am confused by statement in A)

A. (2%3) according to [code.org](http://code.org) is 2  
B. If we divide 2 by 3, the result is .6666666667 (so I am confused by statement in A)

So I am totally confused. Is there a video somewhere that explains the MOD operator? I watched the following video:  
[https://video.search.yahoo.com/search/video;\_ylt=Awr46ue43DdgI3QApAL7w8QF;\_ylu=c2VjA3NlYXJjaAR2dGlkA0MwMjU2;\_ylc=X1MDOTY3ODEzMDcEX3IDMgRhY3RuA2NsawRjc3JjcHZpZANQYnl2dnpFd0xqS3F4YllyWURabGNnQUxOalV1TVFBQUFBQmpxSTEyBGZyA21jYWZlZQRmcjIDc2EtZ3AEZ3ByaWQDZmYzWk9GUjdSZFcxU3BmMVEzUG5MQQRuX3JzbHQDNjAEbl9zdWdnAzAEb3JpZ2luA3ZpZGVvLnNlYXJjaC55YWhvby5jb20EcG9zAzAEcHFzdHIDBHBxc3RybAMEcXN0cmwDMzIEcXVlcnkDd2hhdCUyMGlzJTIwdGhlJTIwTU9EJTIwT3BlcmF0b3IEdF9zdG1wAzE2MTQyNzM2NzA-?p=what+is+the+MOD+Operator&ei=UTF-8&fr2=p%3As%2Cv%3Av%2Cm%3Asa&fr=mcafee#action=view&id=5&vid=c872cf47d15bb82511d04994b3c3c4ce](https://video.search.yahoo.com/search/video;_ylt=Awr46ue43DdgI3QApAL7w8QF;_ylu=c2VjA3NlYXJjaAR2dGlkA0MwMjU2;_ylc=X1MDOTY3ODEzMDcEX3IDMgRhY3RuA2NsawRjc3JjcHZpZANQYnl2dnpFd0xqS3F4YllyWURabGNnQUxOalV1TVFBQUFBQmpxSTEyBGZyA21jYWZlZQRmcjIDc2EtZ3AEZ3ByaWQDZmYzWk9GUjdSZFcxU3BmMVEzUG5MQQRuX3JzbHQDNjAEbl9zdWdnAzAEb3JpZ2luA3ZpZGVvLnNlYXJjaC55YWhvby5jb20EcG9zAzAEcHFzdHIDBHBxc3RybAMEcXN0cmwDMzIEcXVlcnkDd2hhdCUyMGlzJTIwdGhlJTIwTU9EJTIwT3BlcmF0b3IEdF9zdG1wAzE2MTQyNzM2NzA-?p=what+is+the+MOD+Operator&ei=UTF-8&fr2=p%3As%2Cv%3Av%2Cm%3Asa&fr=mcafee#action=view&id=5&vid=c872cf47d15bb82511d04994b3c3c4ce)

---

<div class="post-metadata">

**Author:** ![bhatnagars](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/bhatnagars/32/4919_2.png) [@bhatnagars](https://forum.code.org/u/bhatnagars)\
**Post date:** [February 25, 2021, 6:11pm UTC](https://forum.code.org/t/unit-7-lesson-2-mod-operator/34301/2 "2021-02-25T18:11:36Z")

</div>

When using the mod operator you only do integer division. You stop the division when you start getting decimals. 5/2 divides once and with a quotient of 2 and a remainder of 1. If you divide any further, you will start getting decimals.

Another way to look at this is to keep mulitiplying by 2 till you get as close as you can to 5 without going over. Since 2 x **2** = 4 and 2 x 3 = 6, we stop at 2 get a quotient of 2. What is left over is 5 - 4 = 1. The answer to 5%2 is 1. Let’s look at a bigger number 31 % 2 = 1. Since 2 x **15** is 30 and 2 x 16 = 32, we get a quotient of 15. What is left over is 31 - 30 = 1. That is the remainder.

For 2%3, since we cannot divide 2 by 3, the quotient is 0 and reminder is 2.

Try this video - [What is Modulo? Using the Modulo Operator in Python and Swift Tutorial - YouTube](https://www.youtube.com/watch?v=pNXwzIohx8c)

I hope this helps.

---

<div class="post-metadata">

**Author:** ![gjschmidt](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/gjschmidt/32/4922_2.png) [@gjschmidt](https://forum.code.org/u/gjschmidt)\
**Post date:** [March 1, 2021, 10:46pm UTC](https://forum.code.org/t/unit-7-lesson-2-mod-operator/34301/3 "2021-03-01T22:46:24Z")

</div>

Whenever I go through Mod with my students, I resort to showing examples in good old long division:  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/f/f6a875e34e670f2af43d867b9b5f4026cd43e82a.png)

It is something they instantly recognize and helps them get it.

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [March 2, 2021, 1:52am UTC](https://forum.code.org/t/unit-7-lesson-2-mod-operator/34301/4 "2021-03-02T01:52:49Z")

</div>

> [@CSP U7 L3 level 9](https://forum.code.org/t/csp-u7-l3-level-9/34274):
>
> This is my first year teaching an AP course, and I must admit that I have never heard of n mod n or n % n. I have searched and struggled to find why 2 % 3 is 2 and why 2 % 4 is 2. Not being a math person, I thought that you couldn’t divide 2 by a larger number. There is no remainder! Please help. Code from U7 L3 level 9 example solution: // Make sure students are understanding the concept of how % // is tied to the remainder // Example console.log(5%2); console.log(1); console.log("")…

---

<div class="post-metadata">

**Author:** ![gjschmidt](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/gjschmidt/32/4922_2.png) [@gjschmidt](https://forum.code.org/u/gjschmidt)\
**Post date:** [March 3, 2021, 2:20am UTC](https://forum.code.org/t/unit-7-lesson-2-mod-operator/34301/5 "2021-03-03T02:20:00Z")

</div>

Here is 2 mod 3:  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/f/fb0134d53bac4cb1ddd224be059ab1dc9a46aede.png)

2 mod 4 works the same way.
