Java

How To Divide In Java

In java, the modulus and division operators are used to divide two numbers. The division operator is represented by the “/” symbol, while the “%” symbol represents the modulus operator. The division operator performs division on two numbers and returns the resultant quotient. On the other hand, the modulus operator divides the two numbers and returns the resultant remainder.

In this write-up, you will learn how to divide in java. To do so, you must understand the below-given concepts:

Let’s begin!

How to divide two numbers in Java?

The division operator “/” is one of the four fundamental arithmetic operators. It can be used with any numeric value, such as a floating-point value, integer value, etc.

Example:1 how to divide two integers in Java

int value1 = 300;
int value2 = 250;
int result = value1 / value2;
System.out.println("Resultant output: " + result);
  • Initially, we created two integers and initialized them with 300 and 250, respectively.
  • Next, we created another variable named “result” to store the result.
  • Finally, we utilized the “System.out.println()” statement to print the resultant value:

This is how you can perform the division on two integers using the division operator.

Example:2 How to divide two floating-point numbers?

float value1 = 140.72f;
float value2 = 12.55f;
float result = value1 / value2;
System.out.println("Resultant output: " + result);
  • We created two variables i.e. “value1” and “value2” and assigned them some floating-point values.
  • Next, we created another variable to store the result of “value1/value2”.
  • Finally, we utilized the println() method to show the resultant output:

The output clarified that the division operator works fine on the floating-point values.

How to perform division on user-entered values?

In java, the Scanner class is used to interact with the user. It offers multiple methods to achieve various functionalities; for example, the nextInt() method is used to get an integer value from the user, and the nextLine() method is used to read string data from the user.

First, we must import the Java’s Scanner class to utilize any of its built-in methods:

import java.util.Scanner;

Example: How to perform division on user-entered integers:

Scanner getValue= new Scanner(System.in);
System.out.print("Enter First Number: ");
int number1 = getValue.nextInt();
System.out.print("Enter Second Number: ");
int number2 = getValue.nextInt();
int result = (number1 / number2);
System.out.println("Resultant output: " + result);
  • Initially, we created an object of the Scanner class.
  • Next, we utilized that object with the “nextInt()” method to get the integer values from the user.
  • Afterward, we created a variable “result” to store the result of “number1/number2”.

The output shows that the division operator skips the floating-point value and returns the remaining quotient i.e., “16”.

How to find the remainder of two numbers in Java?

The modulo/modulus operator performs division on two numeric values like int, float, etc. and returns the remainder instead of the quotient.

Example: how does the modulo operator work in Java

Scanner getValue = new Scanner(System.in);
System.out.print("Enter First Number: ");
int number1 = getValue.nextInt();
System.out.print("Enter Second Number: ");
int number2 = getValue.nextInt();
int result = (number1 % number2);
System.out.println("Resultant output: " + result);

We considered the same previous example, but this time we utilized the modulus operator instead of the division operator. Consequently, we will get the below-given output:

The output verified that the modulus operator performed the division on the given numbers and returned the remainder of the given values.

What is a division assignment operator, and how does it work in Java?

The division assignment operator “/=” performs two functionalities in one go, i.e., division and assignment. It performs the division on two operands and assigns the resultant value to the left operand.

Example: Division Assignment Operator

int number1 = 120;
int number2 = 32;
number1 /= number2;
System.out.println("Resultant output: " + number1);
  • We created two integers and assigned them 120 and 32 respectively.
  • Next, we utilized the division assignment operator to perform division and assign the result to the first/left operand.
  • Finally, we utilized the “System.out.println()” to print the output:

The output verified that the division assignment operator performed two functionalities, i.e., “division and assignment” in one go.

Conclusion

Java offers a couple of arithmetic operators to perform the division, such as modulus operator “%” and division operator “/”. The division operator performs division on two numbers and returns the resultant quotient. While the modulus operator divides the two numbers and returns the resultant remainder. This write-up explained how to divide in java; to do that, this article considered a couple of examples for a profound understanding of the concepts.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.