
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Compute Quotient and Remainder
Given an integer a and a non-zero integer d, our task is to write a Java program to compute the quotient and remainder. Quotient can be calculated using the formula "Quotient = Dividend / Divisor" and for remainder, use "Remainder = Dividend % Divisor".
When a number, i.e., dividend, is divided by another number, i.e., divisor, the quotient is the result of the division, while the remainder is what is left over if the dividend does not divide completely by the divisor.
Example Scenario
Suppose our input is -
Input1: Dividend = 50 Input2: Divisor = 3
The output would be -
Output: Quotient = 16 and Remainder = 2
Important!nt! If you pass 0 as divisor, Java will throw ArithmeticException as a number cannot be divided by 0.
Program to Calculate Quotient and Remainder
As mentioned earlier, use the modulus operator (%) to find the remainder and use the division operator (/) for the quotient. A Java program that demonstrates how to compute the quotient and remainder is given below:
public class RemainderQuotient { public static void main(String[] args) { int my_dividend , my_divisor, my_quotient, my_remainder; my_dividend = 50; my_divisor = 3; System.out.println("The dividend and the divisor are defined as " +my_dividend +" and " +my_divisor); my_quotient = my_dividend / my_divisor; my_remainder = my_dividend % my_divisor; System.out.println("The quotient is " + my_quotient); System.out.println("The remainder is " + my_remainder); } }
Output
The dividend and the divisor are defined as 50 and 3 The quotient is 16 The remainder is 2
Quotient and Remainder using User-Defined Function
Here, we use a user-defined method in Java to calculate the quotient and remainder -
public class RemainderQuotient { public static void main(String[] args) { int my_dividend = 47; int my_divisor = 3; System.out.println("The dividend and the divisor are defined as " + my_dividend + " and " + my_divisor); int[] result = calc(my_dividend, my_divisor); System.out.println("The quotient is " + result[0]); System.out.println("The remainder is " + result[1]); } public static int[] calc(int dividend, int divisor) { int my_quotient = dividend / divisor; int my_remainder = dividend % divisor; return new int[]{my_quotient, my_remainder}; } }
Output
The dividend and the divisor are defined as 47 and 3 The quotient is 15 The remainder is 2