Saturday, August 13, 2022

[FIXED] How does reminder operator works in Java (%) when the dividend is less than the divisor?

Issue

I would like to know the specific reason why the following code returns the following output.

int myReminder = 3%10; //note that the dividend (3) does not fit in the divisor(10); System.out.println(myReminder);

output: 3

Why does it returns me 3? I would like an specific reason, thank you!...


Solution

Okay, so sometimes when dividing there is something left over. It is called the remainder.

  • Case 1: 21%10. In this case, you can divide 21 into groups of 10 a maximum of 2 times. So there is a 1 left. So, that is the remainder.

  • Case 2: 3%10. In this case, you can divide 3 into groups of 10 a maximum of 0 times, i.e., you cannot divide it. So, at the end you have 3 left as the remainder.



Answered By - Muhammed Jaseem
Answer Checked By - Marie Seifert (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.