1. Overview
In this tutorial, you'll learn how to find the quotient and remainder when one number is divided by another one.
This is a simple and easy but many students does not get digested easily.
let us understand clearly with the examples.
Understand public static void main(String[] args) method
2. Example Program To Find Quotient and Remainder
In the below example, first created two integer variables number1, number2.
number1 is called as Dividend and second number2 is called as Divisor.
Next, to find the quotient use the '/' forward slash operator. As number1 and number 2 are integers so result also produces as integer.
Finally, to find the remainder use '%' modulo operator and result will be int type.
If you see mathematically, then 100/30 should be 3.33 and it is mix of quotient and remainder values.
To get these values separately in two variables use the following code.
package com.javaprogramto.programs.maths; public class QuotientRemainderExample { public static void main(String[] args) { // Creating int variable for number 1 int number1 = 100; // Creating int variable for number 2 int number2 = 30; // Calculating the quotient int quotient = number1 / number2; // Calculating the remainder int remainder = number1 % number2; System.out.println("Quotient value: "+quotient); System.out.println("Remainder value: "+remainder); } }
Output:
Quotient value: 3.3333333333333335 Remainder value: 10.0
3. Example Program To Get Remainder as ZERO
Let us write a simple program to get the remainder 0 with different inputs.
If the dividend is a multiple of divisor then remainder always will be zero.
public class QuotientRemainderExample { public static void main(String[] args) { // Creating int variable for number 1 int number1 = 200; // Creating int variable for number 2 int number2 = 50; // Calculating the quotient int quotient = number1 / number2; // Calculating the remainder int remainder = number1 % number2; System.out.println("Quotient value: "+quotient); System.out.println("Remainder value with zero: "+remainder); } }
Output
Quotient value: 4 Remainder value with zero: 0
4. Conclusion
In this article, you've seen how to get the quotient and remainder for any two numbers with example programs.
Note: We need to this in two steps and can not find both in a single step.
As usual, All examples are shown on GitHub.
Other Java Programs
No comments:
Post a Comment
Please do not add any spam links in the comments section.