Exercise:
Write a Java Program to calculate quotient and remainder of 2 integers and print the results.
Click Here to View the Solution!
public
class QuotientRemainder {
public static void main(String[] args) {
int dividend =
37, divisor =
5;
int quotient = dividend / divisor;
int remainder = dividend % divisor;
System.out.println(
"Quotient = " + quotient);
System.out.println(
"Remainder = " + remainder);
}
}
Click Here to View the Output!
Quotient = 7
Remainder = 2
Click Here to View the Explanation!
- In this program, two variables
dividend
anddivisor
are declared and then37
and5
are stored in them respectively. - Now, to find the
quotient
we dividedividend
bydivisor
using/
operator. - Since both variables are integers the resultant will be computed as an integer as well.
- Mathematically 37/5 is equal to 7.4, but as we used integer data type to store
dividend
anddivisor
so theint quotient
variable only stores 7 (integer part of answer). - Likewise we used
%
(Modulus) operator to find the remainder. So, the remainder of 37/5, i.e. 2 is stored in an integer in variableremainder
. - Finally
println()
function printsquotient
andremainder
on console screen.