How to divide in java

Java Division Example

In this article, we will have a look at integer java division example to see different ways of how division works.

1. How to perform division in Java

In java, / is the division operator. Depending upon the type of variables fed in the division operator, result of the division can be an integer or a value with precision.

1.1 Integer Division

When both of the variables are of int type or the denominator in the division is int, java performs integer division. Therefore, integer division is the same as a real division but the fraction part of the result is thrown away.

Let’s look at it with an example,

private int integerDivision(int a, int b)< if(b == 0) return 0; else return a/b; >

Calling above method with variable a as 5, b as 3 will result in:

public class JavaDivisionExample

1.2 Precision Division

In some cases, we may need the complete result of the division along with its fraction part. In such scenarios, either both of the variables used for division must be double or float type or at least the denominator must be double or float data type.

Here, as we will see below, the result will consist of a fraction as well. Please note that you should collect the result of the division in a variable of the same data type as the data type of denominator. Without explicit downcasting, trying to store the result in a different type will give a compilation error. Precision Division Method

private int integerDivision(int a, int b)< if(b == 0) return 0; else return a/b; >

We will be passing the same parameters as used above i.e. variable a as 5, b as 3, to see the difference in result. Invocation

public class JavaDivisionExample

Precision Division: 1.6666666666666667

1.3 Remainder

To calculate the remainder, java provides % operator. This operator returns the remainder of the division, if any, otherwise returns 0 if the numerator is a whole multiple of the denominator. Remainder Method

private int getRemainder(int a, int b)

Here, we will use the same parameters again i.e. variable a as 5 and b as 3 to see the result of the method. Invocation

public class JavaDivisionExample

So, the above examples explain how division works in java both with and without precision data types. Also, we saw how can we compute the remainder.

Источник

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.

Читайте также:  Серый градиент фон css

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

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:

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:

Читайте также:  Java установить часовой пояс

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.

Источник

How to Divide in Java with Decimals

This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions.

This article has been viewed 41,711 times.

This wikiHow article will show you three ways to do decimal numbers in Java. If you want to divide two integers (non-decimal) and get a non-rounded answer, cast one of the operands to a double. To divide two decimal numbers (or an integer by a decimal or vice-versa), you can use simple double division to ensure a decimal result. When working with larger numbers or numbers that needs to be extremely precise, you can use the BigDecimal Java class instead of floating point arithmetic.

Get a decimal result from two integers

Image titled 12980637 1

Java, like other programming languages, uses integer division by default. If dividing two integers results in a remainder, the remainder is discarded, leaving you with a whole integer as the answer. If you need to divide two integers and get a decimal result, you can cast either the numerator or denominator to double before the operation. [1] X Research source is performed. In this example, we’ll cast a to double so we get a decimal result:

int a = 55; int b = 25; double r = (double) a / b // The answer is 2.2. 

Divide two decimal numbers (doubles)

Image titled 12980637 2

When you’re dividing a decimal number by another decimal number, you’ll use double division. [2] X Research source Similarly, if one of the two operands is an integer (a non-decimal number), the result will still be a decimal number if the other operand is a double. Here is a simple example of dividing two decimal numbers with double division:

double x = 10.5; double y = 2.5; x / y // the answer is 4.2 

Get the most precise decimal results with BigDecimal

Image titled 12980637 3

If you’re working with currency or need the most precise decimal result, use the BigDecimal class. Floating point arithmetic (which is what you’re doing with double ) is less precise, as double stores numbers as binary representations of fractions and exponents instead of exact representations (fixed-point numbers). [3] X Research source To ensure you’re working with fixed-point numbers, use BigDecimal . In this example, we’ll use BigDecimal to divide two numbers for a precise result:

BigDecimal bdec = new BigDecimal("706"); BigDecimal bdecRes = bdec.divide(new BigDecimal("20")); System.out.println("Divide: " + bdecRes); // Divide with MathContext MathContext mc = new MathContext(2, RoundingMode.FLOOR); BigDecimal bdecMath = bdec.divide(new BigDecimal("20"), mc); System.out.println("Divide with MathContext: " + bdecMath); // the first result will be 45.25, and the second will be 45. 
  • When you use BigDecimal, you’ll need to specify the RoundingMode for the result, which can be UP (away from zero), DOWN (toward zero), CEILING (toward positive infinity), FLOOR (toward negative infinity), HALF_UP (toward nearest neighbor, or up if both neighbors are equal), HALF_DOWN (toward nearest neighbor, or down if equal), HALF_EVEN (toward nearest neighbor, or to the nearest even neighbor if equal), or UNNECESSARY (result should be exact). [4] X Research source
Читайте также:  Спектральный анализ изображений python

Источник

Divide two numbers without using Arithmetic operator in Java

Divide two numbers without using Arithmetic operator in Java

In this tutorial, we will discuss the concept of Divide two numbers without using Arithmetic operator in Java

In this topic, we are going to learn how to divide two numbers without using Arithmetic operator in Java language

Divide of two numbers without using arithmetic operator in Java

What is division

The division is a method of splitting a group of things into equal parts. The division is an arithmetic operation inverse of multiplication

It is one of the four basic operation of arithmetic calculation others being addition,subtraction,multiplication

The division of two natural numbers means it is the operation of calculating the number of times one number is contained within one another

Java exercise to Divide two numbers

Program to division of two numbers without division operator

The program allows the user to enter two integer numbers and then it calculates the division of the given numbers without using the division operator in Java language

import java.util.Scanner; class Divisionwithoutdivope2< public static void main(String args[])=num2) < num1=num1-num2; result++; >System.out.println("Division is:"+result); > >

When the above code is executed, it produces the following result

Enter the value to num1: 500 Enter the value to num2: 25 Division is: 20

Program to division of two numbers using Bitwise operator

The program allows the user to enter two integer numbers and then it calculates the division of the given numbers using with the Bitwise operator in Java language

import java.util.Scanner; class DivisionwithBitwise < public static void main(String args[])while(temp>1) < num2>>=1; temp>>=1; if(num1>=num2) < num1-=num2; result+=temp; >> System.out.println("Division is:"+result); > >

When the above code is executed, it produces the following result

Enter the value to num1: 650 Enter the value to num2: 25 Division is: 26

Suggested for you

Источник

Оцените статью