English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this program, you will learn how to calculate the quotient and remainder from the given dividend and divisor in Java.
public class QuotientRemainder { public static void main(String[] args) { int dividend = 25, divisor = 4; int quotient = dividend / divisor; int remainder = dividend % divisor; System.out.println("Quotient = ", + quotient); System.out.println("Remainder = ", + remainder); } }
When the program is run, the output is:
Quotient = 6 Remainder = 1
In the above program, two numbers25(dividend)and4(divisor)are stored in two variables dividend and divisor respectively.
Now, to find the quotient, we use / operator divides dividend by divisor. Since both dividend and divisor are integers, the result will also be calculated as an integer.
So, mathematically25/4The result is6.25But since both operands are int, the variable quotient only stores6(integer part).
Similarly, to find the remainder, we use the % operator. Therefore, the25/4The remainder (i.e.,1)is stored in the integer variable remainder.
Finally, use the println() function to print the quotient and remainder on the screen.