14. Remainder Operator


Answer:

3

Remainder Operator

You may recall how in grade school you did division like this:

13 divided by 5

13 / 5 == 2 with a remainder of 3.

This is because 13 == 2*5 + 3.

The symbol for finding the remainder is % (percent sign). If you look in the operators, table of table of operators you will see that it has the same precedence as / and *. Here is a program:


public 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.

Question 14:

Will the following always print out the original integer, regardless of what it was?

    System.out.println("The original : " + (quotient*3 + remainder) );