Please explain why 17 @ = 17

前端 未结 6 2015
[愿得一人]
[愿得一人] 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: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.

提交回复
热议问题