Java Program to Compute Quotient and Remainder

Configurare noua (How To)

Situatie

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);
  }
}

Output:

Quotient = 6
Remainder = 1

Backup

In the above program, we have created two variables dividend and divisor. We are calculating the quotient and remainder by dividing 25 by 4.

To find the quotient, we have used the / operator. We have divided dividend 25 by divisor 4. Since both dividend and divisor are integers, the result will also be integer.

25 / 4 // results 6.5
// convert 6.5 to integer
// output will be 6

Likewise, to find the remainder we use the % operator. The dividend is divided by the divisor and the remainder is returned by the % operator.

25 % 4 // results 1

Quotient and remainder are printed on the screen using println() function.

Solutie

Tip solutie

Permanent

Voteaza

(7 din 10 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?