Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does modulus of a smaller dividend and larger divisor work?

Tags:

c

math

7 % 3 = 1 (remainder 1)

how does
3 % 7 (remainder ?)

work?

like image 918
T.T.T. Avatar asked Sep 01 '10 21:09

T.T.T.


People also ask

How does modulo work with smaller numbers?

If the numerator is smaller than the denominator, then the remainder is equal to the numerator. 3 % 10 =3 //If smaller number i.e. 3 is divided by bigger number i.e. 10, then the numerator becomes the remainder.

What happens when the dividend is smaller than the divisor?

The most simple and effective catch to remember would be: Whenever dividend is less than the divisor, modulus is just that dividend.

How does modulus division work?

The modulo operation (abbreviated “mod”, or “%” in many programming languages) is the remainder when dividing. For example, “5 mod 3 = 2” which means 2 is the remainder when you divide 5 by 3.

Can the result of a modulo operation be larger than the divisor?

Answer. No, the result of a modulo operation can never be larger than the divisor, assuming all positive values being used in the operation. If the division is done correctly, then the divisor was fitted into the dividend as many times as possible.


9 Answers

remainder of 3/7 is 3..since it went 0 times with 3 remainder so 3%7 = 3

like image 163
Jesse Naugher Avatar answered Oct 04 '22 21:10

Jesse Naugher


7 goes into 3? zero times with 3 left over.

quotient is zero. Remainder (modulus) is 3.

like image 20
S.Lott Avatar answered Oct 04 '22 22:10

S.Lott


The same way. The quotient is 0 (3 / 7 with fractional part discarded). The remainder then satisfies:

(a / b) * b + (a % b) = a
(3 / 7) * 7 + (3 % 7) = 3
0 * 7 + (3 % 7) = 3
(3 % 7) = 3

This is defined in C99 §6.5.5, Multiplicative operators.

like image 32
Matthew Flaschen Avatar answered Oct 04 '22 23:10

Matthew Flaschen


Conceptually, I think of it this way. By definition, your dividend must be equal to (quotient * divisor) + modulus

Or, solving for modulus: modulus = dividend - (quotient * divisor)

Whenever the dividend is less than the divisor, the quotient is always zero which results in the modulus simply being equal to the dividend.

To illustrate with OP's values:

modulus of 3 and 7 = 3 - (0 * 7) = 3

To illustrate with other values:

1 % 3:
1 - (0 * 3) = 1

2 % 3:
2 - (0 * 3) = 2
like image 29
Gregory Sandoval Avatar answered Oct 04 '22 22:10

Gregory Sandoval


  • 7 divided by 3 is 2 with a remainder of 1

  • 3 divided by 7 is 0 with a remainder of 3

like image 21
racetrack Avatar answered Oct 04 '22 22:10

racetrack


As long as they're both positive, the remainder will be equal to the dividend. If one or both is negative, then you get reminded that % is really the remainder operator, not the modulus operator. A modulus will always be positive, but a remainder can be negative.

like image 39
Jerry Coffin Avatar answered Oct 04 '22 23:10

Jerry Coffin


(7 * 0) + 3 = 3; therefore, the remainder is 3.

like image 29
NawaMan Avatar answered Oct 04 '22 22:10

NawaMan


a % q = r means there is a x so that q * x + r = a.

So, 7 % 3 = 1 because 3 * 2 + 1 = 7,

and 3 % 7 = 3 because 7 * 0 + 3 = 3

like image 35
small_duck Avatar answered Oct 04 '22 23:10

small_duck


It seems you forgot to mention the surprising case, if the divident is smaller and negative:

-3 % 7 

result: 4
like image 44
Vertux Avatar answered Oct 04 '22 22:10

Vertux