You code is actually doing
remainder = 3 % 7; // equals 3.
The best way to determine why your code is not doing what you think is to step through your code using a debugger.
All the multiples of 3 & 7 will be multiples of 21, i.e. 21, 42, 63, 84.
Answer from Peter Lawrey on Stack OverflowYou code is actually doing
remainder = 3 % 7; // equals 3.
The best way to determine why your code is not doing what you think is to step through your code using a debugger.
All the multiples of 3 & 7 will be multiples of 21, i.e. 21, 42, 63, 84.
Your 3 is getting tacked onto the end of the line of text above. You'll be seeing
Integers less than 100 that are
evenly divisible by 3 or 73
because you wrote print instead of println for this line of text. The % operator is working just fine, and 3 % 3 is indeed 0, not 3.
modulo - What's the syntax for mod in Java? - Stack Overflow
How does the modulus operator work?
modular arithmetic - Not understanding modulo - Mathematics Stack Exchange
What the HELL is a modulo?
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;
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.
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.
If you mean Modulo operation like "3 mod 2" then this is just the remainder when you divide 3 by 2. 3 divided by 2 is 1 with a remainder of 1, so "3 mod 2" is 1.
In order to understand this, you must first be able to do long division by hand (using calculators doesn't work since you get decimal numbers). If you know long division, then it's really easy to calculate any number modulo any other number (natural numbers only).
For example 4 mod 2 is 0 because 4 is divisible by 2.
16 mod 3 is 1 because 16 divided by 3 is 5 with a remainder of 1. In other words 3 goes into 16 5 times and there is a remainder of 1.
It's all about remainders! Once you get comfortable with the definition you can learn some algebraic rules of manipulating these remainders.
When you first learned about division, it might even have been before you learned about fractions. If you are dividing integers ("whole numbers") then instead of writing $14 \div 3 = 4\frac{2}{3}$ you can write $$14 \div 3 = 4 \text { remainder } 2.$$
The modulo operator is just the function that takes two integers and gives the remainder when the first is divided by the second: $$14 \mod 3 = 2, \quad$$
The other operator of integer division is usually called $\text{div}$, and $x \text{ div } y$ is defined, as you'd expect, to be the greatest $n$ such that $y \times n \leq x$. So for example $14 \text{ div }3 = 4$. And we can define $\text{mod}$ formally as: $$x \mod y := x- (y \times (x \text{ div } y)).$$
Is this clear?