English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java Other Topics

Java program to calculate quotient and remainder

Java Examples Comprehensive

In this program, you will learn how to calculate the quotient and remainder from the given dividend and divisor in Java.

Example: Calculate quotient and remainder

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.

Java Examples Comprehensive