| by Arround The Web | No comments

Integer Division Java

While dealing with mathematical computations in Java, there can be instances where the developer is required to compute the “division” of the numbers. For instance, simplifying the complex values or omitting the decimal values from the code. In such scenarios, the “Integer division” in Java assists in streamlining the code functionalities at the programmer’s end.

This blog will illustrate the implementation of the “Integer Division” in Java.

How to Perform “Integer Division” in Java?

In Java, the integer division can be carried out with the help of the “Arithmetic Operator ( / )”. This operator can return the division of the specified or the user-input integers.

Example 1: Performing Division Upon the Specified Integers

In this example, the two specified integers can be computed for division:

int num1 = 25;

System.out.println("The dividend is: " + num1);

int num2 = 5;

System.out.println("The divisor is: " + num2);

int result = num1 / num2;

System.out.println("The division of the numbers become: " + result);

In the above code block, apply the following steps:

  • First, initialize the first integer value, i.e., “Dividend”, and display it.
  • Likewise, display the latter initialized integer, i.e., “Divisor”.
  • Now, apply the “Arithmetic Operator( / )” between the dividend and divisor to compute the division and display the resultant outcome on the console.

Output

In the above output, it can be analyzed that the resultant number is displayed after division.

Example 2: Performing Division Upon the User Input Integers

In this specific example, the user input integers can be computed for division. Before heading to the example, include the below-given library to enable user input:

import java.util.Scanner;

Add the following code in the “main()” method:

Scanner input = new Scanner(System.in);

System.out.println("Enter the dividend: ");

int num1 = input.nextInt();

System.out.println("Enter the divisor: ");

int num2 = input.nextInt();

System.out.println("The division becomes: "+ num1/num2);

In this code, perform the following steps:

  • Create a “Scanner” object named “input” via the “new” keyword and the “Scanner()” constructor, respectively.
  • Note that the “System.in” parameter refers to the user input.
  • In the next step, associate the “nextInt()” method with the created object to ensure that the user input is “integer”.
  • Finally, apply the “Arithmetic operator ( / )” to return the division of the user input numbers.

Output

Case 1: Remainder Equals “0”(Completely Divisible)

In this outcome, it is evident that the real-time division is returned based on the user input numbers.

Case 2: Remainder Not Equals “0”

In the case of division where the remainder is not equivalent to “0”, the final result will be rounded off to the largest divisible integer, as follows:

As observed, the largest rounded integer is returned.

Conclusion

In Java, integer division can be carried out with the help of the “Arithmetic Operator ( / )”. This is done by returning the corresponding or the largest divisible integer(in the remainder case). The division can be performed upon the specified or the user input integers. This blog discussed the implementation of the “Integer division” in Java.

Share Button

Source: linuxhint.com

Leave a Reply