# Java and type int

Quotients and remainders can be calculated easily in Java using the operators / and %

Division with integers in Java gives an integer answer, which means that if $a$ and $b$ have type int then $a / b$ is an integer and so might be an approximation of the number $\frac{a}{b}$ . Integer
division truncates to give an integer. For example, $\frac{14}{5} = 2.8$, but using the operator / with type int gives $2$. That is, the answer given is $2.8$ truncated to the integer $2$.

Another operator, %, known as the modulus or remainder operator, is used with two integers and $a\% b$ returns the remainder when $a$ is divided by $b$. For example $14\% 5$ is $4$ because $14$ divided by $5$ leaves are remainder of $4$.

let's revisit some earlier examples, but using the operators % and / with type int.

##### Example 1

In the egg example with $22$ eggs put into boxes of $6$, the remainder (the number left over) is $22\% 6=4$.

##### Example 2

How many years and months are there in $18$ months?

There are $12$ months in a year, so we divide by $12$. Using type int we obtain $18/12=1$ year and $18\%12=6$ months.

##### Example 3

How many minutes and seconds is $134$ seconds?

We divide by $60$ because there are $60$ seconds in a minute. The answer is $134/60=2$ minutes and $134\% 60=14$ seconds.

##### Example 4

How many weeks and days is $25$ days?

The answer is $25/7=3$ weeks and $25\% 7=4$ days.

Many simple examples do not use negative numbers, but sometimes negative numbers are needed. In Java the number returned for $a\%b$ is $a-(a/b)\times b$. this is true for both positive and negative numbers. For example, $-14\%5=-14-(-14/5)\times 5=-14-(-2)\times 5=-4$.