-2
$\begingroup$

I know what mod does it gives you the remainder but why is it that (smaller integer) % (bigger integer) = smaller integer e.g ( 3%5 = 3 ) or
( 4%5= 4 ) why does this happen i cant figure it out

$\endgroup$
5
  • $\begingroup$ Because that's the remainder. $3$ divided by $5$ gives a quotient of $0$ and a remainder of $3$ (i.e. $0\times 5 + 3 = 3$). Also, this has nothing to do with C (and normally questions about specific programming languages are off topic here, for future reference). $\endgroup$ May 4, 2016 at 5:40
  • 2
    $\begingroup$ Please don't delete your post when someone has taken the time to write an answer. $\endgroup$
    – D.W.
    Nov 20, 2016 at 17:25
  • $\begingroup$ @LukeMathieson Would you kindly write your answer in the answer section instead of comments and elaborate it more I am sorry I know its simple maths but my concept of it is more like 3 divided by 5 gives a quotient of 0.6 and a remainder of 0 :/ again sorry to ask $\endgroup$ Nov 20, 2016 at 17:30
  • 1
    $\begingroup$ @Kingrocker Why are you using the modulus function if you don't even know what it is an integer division? $\endgroup$ Nov 20, 2016 at 17:39
  • $\begingroup$ @Kingrocker, done, with some elaboration. $\endgroup$ Nov 21, 2016 at 0:17

2 Answers 2

5
$\begingroup$

Notice that with the mod operator ($\%$), you're using integer division, much as you are when you use the division operator ($/$) with two ints (at least in most (all?) C style languages). So $3/5$ gives the result of $0$, $7/5$ gives $1$. It's essentially the old kindergarten division - "how many times does five go into three".

Now $\%$ then provides the other half of integer division; five goes into three zero times with three left over, i.e. the remainder of $3/5$ is $3$.

So between $/$ and $\%$, we can get the two results from integer division that solve the following equation: $$x\times5 + y = 3$$

$/$ gives $x$ and $\%$ gives $y$.

$\endgroup$
2
$\begingroup$

Hint: By definition.

$$a\mod n \equiv a, $$ when $0 \leq a <\leq n$.

The congruence $b \equiv a\mod n$ actually means that exists a $k,b\in \mathbb{N}$ such that $n*k = b - a$. Then, for the statement above, we have $a - a = 0 = 0*n$, with $b=a$, and $k = 0$.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.