0

I understand modulus returns the remainder, and I tend to look at it like this: 13 % 5 = 3, 5 goes into 13 two times, with three left over

but how come

1 % 5 = 1 2 % 5 = 2 3 % 5 = 3 

5 goes into 1 once, with 0 left over right?

Just trying to see how I should be looking at this to get an understanding, thank you

2
  • 1
    5 goes into 1 once, with 0 left over right? Nope. 5 doesn't go into 1 at all. Nor into 2 nor into 3. They're all smaller than 5. :) Commented Apr 26, 2020 at 1:33
  • Lmao thank you, I'm having a major brain fart Commented Apr 26, 2020 at 2:30

4 Answers 4

2

The remainder in 1%5 refers to what remains of 1 (not 5) after you divide by 5. 5 goes into 1 zero times. So -- when you remove 0 multiples of 5 from 1, all of 1 remains. Thus 1 % 5 = 1.

Refer to this for a even better understanding

Sign up to request clarification or add additional context in comments.

Comments

1

The modulus operator returns the remainder after the division.

For example,

int i = 13 % 5; 

will set i to 3.

Comments

1

Great Question!

Here are some interesting facts of the % (modulus) operator:

  • The remainder operator can be used to get the last digit of any number (based on the number system you use); like dividend % 10 will give you the decimal number system last digit.

  • In java, it can be used with any numeric data types (like byte, short, int, long, float and double).

  • It works with both (positive and negative) numbers. And the result of the negative number is decided by the sign of the dividend; means if the dividend is positive the result will be positive and if the dividend is negative, the result will also be negative. Like 20 % (-3) = 2 but -20 % (-3) = -2 (dividend -20 is negative).

The answer to your question and interesting fact (extracted from the link below):

  • The remainder operation for operands that are integers after binary numeric promotion produces a result value such that (a/b)*b+(a%b) is equal to a.

    This identity holds even in the special case that the dividend is the negative integer of largest possible magnitude for its type and the divisor is -1 (the remainder is 0).

    It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the magnitude of the result is always less than the magnitude of the divisor.

    If the value of the divisor for an integer remainder operator is 0, then an ArithmeticException is thrown.

See what java says https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3

  • Mostly used in the circular array and hashtable to contain elements in limit.

1 Comment

"if the dividend is positive the result will be positive and if the dividend is negative, the result will also be negative" <- that depends. Conventions differ, and different languages do this in different ways. In Python, 20 % (-3) is -1. The OP didn't specify a language.
0

From Euclidian division, we know that for every two integers a and b (b!=0), there exist unique integers q and r such that a = b * q + r and 0 <= r < |b|.

Now, %, the modulo operator, returns the r. For your examples:

 1 % 5: 1 = 5 * 0 + 1 => r = 1 2 % 5: 2 = 5 * 0 + 1 => r = 2 13 % 5: 13 = 5 * 2 + 3 => r = 3 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.