Программа для поиска Quotient And Remainder в Java

Опубликовано: 5 Марта, 2022

Остаток - это целое число, оставшееся после деления одного целого числа на другое. Частное - это количество, произведенное делением двух чисел.

Например,

(7/2) = 3

In the above expression 7 is divided by 2, so the quotient is 3 and the remainder is 1.

Подход : разделите дивиденд на делитель с помощью оператора /. И делимое, и делитель могут быть любого типа, кроме строкового, результат также будет вычислен соответственно. Получите остаток с помощью оператора%.

Выражения, используемые в программе для вычисления частного и остатка:

частное = дивиденд / делитель;
остаток = делитель% дивиденда;

Примечание . Программа выдаст исключение ArithmeticException: / на ноль при делении на 0.

Пояснение :

Consider Dividend = 100 and Divisor = 6

Therefore,
Quotient = 100/6 = 16
Remainder = 100%6 = 4

Нижеприведенные программы иллюстрируют вышеуказанный подход:

Program 1:

public class QuotientAndRemainder {
  
    public static void main(String[] args)
    {
  
        int dividend = 556, divisor = 9;
  
        int quotient = dividend / divisor;
        int remainder = dividend % divisor;
  
        System.out.println("The Quotient is = " + quotient);
        System.out.println("The Remainder is = " + remainder);
    }
}
Output:
The Quotient is = 61
The Remainder is = 7

Program 2: For a negative number.

public class QuotientAndRemainder {
  
    public static void main(String[] args)
    {
  
        int dividend = -756, divisor = 8;
  
        int quotient = dividend / divisor;
        int remainder = dividend % divisor;
  
        System.out.println("The Quotient is = " + quotient);
        System.out.println("The Remainder is = " + remainder);
    }
}
Output:
The Quotient is = -94
The Remainder is = -4

Program 3: This program will throw an ArithmeticException as the divisor = 0.

public class QuotientAndRemainder {
  
    public static void main(String[] args)
    {
  
        int dividend = 56, divisor = 0;
  
        int quotient = dividend / divisor;
        int remainder = dividend % divisor;
  
        System.out.println("The Quotient is = " + quotient);
        System.out.println("The Remainder is = " + remainder);
    }
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at QuotientAndRemainder.main(QuotientAndRemainder.java:7)