Write a program to find remainder of dividing two numbers, without using % operator? In Java

后端 未结 5 1016
难免孤独
难免孤独 2021-01-03 02:21

How to find the remainder of dividing two numbers without using the modulo operator!! My teacher gave me this exact exercise and It\'s only my 5th lecture in a course calle

5条回答
  •  無奈伤痛
    2021-01-03 02:36

    You need to take the Math.floor of a/b.

    you have a%b = a - (a/b)*b but really a%b= a- (Math.floor(a/b)*b)

    This is because without the math.floor it includes the decimal remainder and (a/b)*b is equal to a so when you subtract it from a you will get 0 every time, just as you experienced.

提交回复
热议问题