Remainder Operator

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

What is the remainder after dividing 13 by 5?

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

QUESTION 14:

Why were the innermost set of parentheses used in the statement:

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