Please explain why 17 @ = 17

前端 未结 6 2004
[愿得一人]
[愿得一人] 2021-01-28 06:22

I am new to Java, actually programming in general. I understand that the modulus operator (%) returns the remainder of two numbers, however, I do not understand why 17 % 40 = 1

相关标签:
6条回答
  • 2021-01-28 06:33

    i guess learning back the 3rd and 4th standard maths is the key point.

    if u see (hope understand the division syntax. its the popular 3rd std way )

      ____
    40)17
    

    you will get a reminder 17 as 17 is not divisible by 40. then there will be an adition of '.' and then the fraction will be added

    0 讨论(0)
  • 2021-01-28 06:35

    Maybe this is a different and more helpful way to think about it.

    When we apply division to integer numbers a and b, we are really trying to relate a and b like this:

    a = Q * b + R
    

    a is some multiple of b, plus some leftover. Q and R are integers; to keep this simple, let's also just think of non-negative numbers. The multiple, Q, is the quotient and leftover, R, is the remainder -- the smallest ones that make this relation work.

    In most languages, a / b gives you Q, and and a % b gives you R. (In fact processors tend to compute both at once -- these are so related.)

    So if a is 17 and b is 40, it only works if you write:

    17 = 0 * 40 + 17
    

    This is why a % b must be 17.

    (Note that it gets more complex when considering negative numbers.)

    0 讨论(0)
  • 2021-01-28 06:37

    Equation from Wiki by Knuth:

    enter image description here

    a = 17
    n = 40
    floor(a/n) = 0
    so r = 17
    

    When n > a then r is simply a.

    0 讨论(0)
  • 2021-01-28 06:40

    Java has one important arithmetical operator you may not be familiar with, %, also known as the modulus or remainder operator. The % operator returns the remainder of two numbers. For instance 10 % 3 is 1 because 10 divided by 3 leaves a remainder of 1.

    So in your case 17/40 will leave remainder 17 so result is 17.

    Its same as Like.

    1%10 = 1
    
    0 讨论(0)
  • 2021-01-28 06:46

    If you have the numbers a and b, their quotient q and remainder r, then the following has to be true:

    q · b + r = a

    That is, if you multiply the quotient (q) by the divisor (b) and add the remainder (r), the result is the dividend (a).

    In your case a = 17, b = 40, q = 0 and so r has to be 17.

    Note: the equation above is just a rearrangement of the equation from Nikolay Kuznetsov's answer, but I think it's easier to understand this way.

    0 讨论(0)
  • 2021-01-28 06:52

    When you divide 17/40, quotient is 0 and the remainder is 17.

    The modulo operator (%) returns the remainder.

    i.e

    a % b = remainder of a / b
    
    0 讨论(0)
提交回复
热议问题