What is the remainder after dividing 13 by 5?
3
You may recall how in grade school you did division like this:
13 / 5 == 2
with a remainder of 3
.
This is because 13 == 2*5 + 3
.
The symbol for finding the remainder is %
(percent sign).
This symbol is also called the modulo operator.
If you look in the
table of operators
you will see that it has the same precedence as /
and *
.
Here is a program that is worth study:
class RemainderExample { public static void main ( String[] args ) { int quotient, remainder; quotient = 17 / 3; remainder = 17 % 3; System.out.println("The quotient : " + quotient ); System.out.println("The remainder: " + remainder ); System.out.println("The original : " + (quotient*3 + remainder) ); } }
Copy this program to a file and play with it. Change the 17 and 3 to other numbers and observe the result.
Why were the innermost set of parentheses used in the statement:
System.out.println("The original : " + (quotient*3 + remainder) );