tags:

views:

143

answers:

7
7 % 3 = 1 (remainder 1)

how does
3 % 7 (remainder ?)

work?

+11  A: 

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

Jesse Naugher
@Jesse Naugher: thanks got it...
Tommy
+3  A: 

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.

Matthew Flaschen
+2  A: 

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

quotient is zero. Remainder (modulus) is 3.

S.Lott
+1  A: 

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

NawaMan
+1  A: 

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

small_duck
+1  A: 

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.

Jerry Coffin
A modulus can be negative. You can choose _any_ coset represenative.
aaronasterling
Algebraically, the remainder is always positive. C is simply wrong in this regard, and it's an endless source of trouble. :-(
R..
Regardless of you prefer to define your terms, `%` (as it's defined in C and C++) can produce a negative result, which many people don't expect.
Jerry Coffin
+2  A: 
  • 7 divided by 3 is 2 with a remainder of 1

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

racetrack