Instead of the modulo operator, which has slightly different semantics, for non-negative integers, you can use the remainder operator %. For your exact example:
if ((a % 2) == 0)
{
isEven = true;
}
else
{
isEven = false;
}
This can be simplified to a one-liner:
isEven = (a % 2) == 0;
Answer from Cody Hatch on Stack OverflowInstead of the modulo operator, which has slightly different semantics, for non-negative integers, you can use the remainder operator %. For your exact example:
if ((a % 2) == 0)
{
isEven = true;
}
else
{
isEven = false;
}
This can be simplified to a one-liner:
isEven = (a % 2) == 0;
Here is the representation of your pseudo-code in minimal Java code;
boolean isEven = a % 2 == 0;
I'll now break it down into its components. The modulus operator in Java is the percent character (%). Therefore taking an int % int returns another int. The double equals (==) operator is used to compare values, such as a pair of ints and returns a boolean. This is then assigned to the boolean variable 'isEven'. Based on operator precedence the modulus will be evaluated before the comparison.
How does the modulus operator work?
The Modulus Operator is Powerful!
Videos
I apologise in advance if this is a stupid question, but I've looked everywhere and every explanation I've found has been way above my pay grade.
I've been studying java for a grand total of one week. My lecturer gave us notes that cover the modulus, but after reading them I'm still none the wiser. The examples he gives don't seem consistent and I'm just getting more and more confused.
The exercise involves trying convert 174 pounds into stone and pounds using the modulus, but I honestly have no idea where the modulus goes or how it works, or what numbers I'm supposed to put where. I'd be grateful for literally any explanation at this point.
This may be obvious to most of you, but recently I've been working through the Computer Science textbook from Princeton to fill in knowledge gaps and so far the modulus operator has been used a lot.
I can't think of a single case where I used the modulus operator in any of my school projects and I really wish I knew then what I know now. It may seem simple, but this little guy is incredibly useful! Below are two examples that really stood out to me.
There is an exercise where you need to sum the digits of an integer from right to left, and it's really as simple as taking the result of n % 10 and then performing the calculation because the remainder is the right most digit. Then to get the next digit you just need to n /= 10 until n == 0. Most of you may have known this, but my mind was blown. My solution was not nearly as elegant.
Another exercise has you iterate from 1000 to 2000 and printing only 5 results per line. Again...this is as simple as checking if i + 1 % 5 == 0 before printing. There's no need for some kind of sentinel variable or anything fancy...just the modulus operator checking if 5 divides the iterator evenly.
Sorry if this is known to everyone, but I wanted to share these two things with you because nobody I know cares, and from now on I am going to try and work the modulus operator into my code more often.
The problem here is that in Python the % operator returns the modulus and in Java it returns the remainder. These functions give the same values for positive arguments, but the modulus always returns positive results for negative input, whereas the remainder may give negative results. There's some more information about it in this question.
You can find the positive value by doing this:
int i = (((-1 % 2) + 2) % 2)
or this:
int i = -1 % 2;
if (i<0) i += 2;
(obviously -1 or 2 can be whatever you want the numerator or denominator to be)
Since Java 8 you can use the Math.floorMod() method:
Math.floorMod(-1, 2); //== 1
Note: If the modulo-value (here 2) is negative, all output values will be negative too. :)
Source: https://stackoverflow.com/a/25830153/2311557