r/javahelp • u/macetfromage • Mar 27 '21
Solved 1%2=1?
i dontunderstand the results when the nr to the right is larger
ofc 4%3=1
but why 3%4=3
17
u/rQbTheMan Mar 27 '21 edited Mar 27 '21
% stands for modulus. The answer of doing a % b is the remainder of dividing a by b. 1%2 is basically 1 divided by 2 and the remainder of that would be 1
Forgot to add: you multiply the result of 1/2 by the divisor(2) so its 0.5 x 2 =1. For 3%4 it would be 3/4 = 0.75, then 0.75 x 4 = 3 so 3%4 = 3
17
13
Mar 27 '21
If 1 is unable to be divided by 2, 1 is left over.
If 4 is divisible by three, divide. 4-3 = 1. If 1 is not divisible by 3, then 1 is left over.
2
Mar 27 '21
% (modulus) is the remainder operator. So basically if X is divisible by Y evenly it will return 0.
10 % 2 = 0 or 10/2 = 5 with no remainder.
3 ÷ 4 = 0.75
3 things cannot be divided evenly between 4 people so the remainder is 3.
20 % 2 = 0 or 10 ÷ 2 = 10 with nothing left over. 20 candies can be shared evenly between 10 people without having anything left over. 0 remainder.
The modulus operator returns the remainder of the two numbers after division. If you are provided with two numbers, say, X and Y, X is the dividend and Y is the divisor, X mod Y is there a remainder of the division of X by Y.
3
1
Mar 28 '21 edited Mar 28 '21
% sign is the modulus operator. It gives you the remainder amount. Basically it displays the left over amount from a division
In division 4 / 4 = 1
but using modulo division...
4 % 4 = 0 because 4 divides into 4 perfectly, there is no remainder
Therefore 3 % 4 = 3 because 3 divides into 4 zero times with a remainder left over of 3
4 % 3 = 1 because 4 divides into 3 three times with a remainder left over of 1
-7
u/macetfromage Mar 27 '21
after googling i got the impression that % removes the decimals but i think its in math
1
-5
u/FilsdeJESUS Mar 27 '21
3%4 = 3 how this is even possible ?
3
u/ATE47 Intermediate Brewer Mar 27 '21
3 = 4 * 0 + 3 (n = d * k + r), so we have 3 / 4 = 0 and 3 % 4 = 3
1
u/FilsdeJESUS Mar 27 '21
Ah yes it is a modulo operator 😂 the rest of the operation will be 3 that’s it
3
u/ATE47 Intermediate Brewer Mar 27 '21
It’s not really the modulus, it’s the remainder.
-2 mod 3 = 1
-2 % 3 = -2
(In Java, in other programming language it can be the modulus)
•
u/AutoModerator Mar 27 '21
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.