Div function in java

Integer Division in Java

In this tutorial, we will learn about integer division in Java. Let’s say we have two variables of integer type a=25 and b=5 and we want to perform division.

When it comes to a decision of maintaining precision or avoiding precision mainly at the time of division because while doing division there are high chances of losing precision.

Example of simple division

In the given example we have three int type variables a , b , c and we are storing division of a & b into c . We recommend you to run this example and observe the output and match your observations with whatever we discussed below.

Читайте также:  Jira rest client java

In the above case, the division will work finely because all the variables are in the form of an integer and 5 divide 25 completely. Think about 10/3 definitely c cannot store 3.3333. as it is an int type of a variable and able to store integer data only and it will hold just 3 . In the below case it is not considering a reminder.

Example: losing data

Here, we are dividing two integers and storing the result into int that leads to loss of data because integer does not hold decimal place values.

Now let’s bring the idea of doing real division where we will store the value in double.

Example

In the code given below, to observe the output of what happens when we change the datatype of variables from float to int and vice versa. You will get integers or real numbers.

Conclusion:

After implementing all the above codes we can conclude some important observation based on the datatype of divisor and dividend

Источник

How to Do Division in Java (Integer and Floating Point)

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 62,236 times.

There are two types of division in Java—integer division and floating-point division. Both types use the forward slash (/) symbol as the operator, following the format dividend / divisor. Read on to learn how to divide two integers (non-decimal whole numbers) to receive an integer quotient, and how to use floating point division to get a decimal result.

Integer Division

Image titled 12966108 1

When you divide two integers in Java, the fractional part (the remainder) is thrown away. For example, if you were to divide 7 by 3 on paper, you’d get 2 with a remainder of 1. But when you divide two integers in Java, the remainder will be removed and the answer will just be 2. [1] X Research source To use integer division, you’d use this syntax:

int a = 7; int b = 3; int result = a / b; // result will be 2 
  • Dividing two integers always results in an integer. If you want to get a decimal result from dividing two integers, use floating point division.
  • If you try to divide an integer by 0 using integer division, you’ll get an ArithmeticException error at runtime, even if the program compiles fine. [2] X Research source

Floating point division

Image titled 12966108 2

If either operand in the equation is of the float or double type, you’ll need to use float division. You can also use float division if you are dividing two numbers and want a decimal result. To use this division type, set the dividend and divisor to a float. [3] X Research source With our example of 7 divided by 3, our code would look like this:

float a = 7.0f; float b = 3.0f; int result = a / b; // result will be 2.33 
  • When dividing by zero with floating point division, the result will be NaN (Not a Number). [4] X Research source

Expert Q&A

When dividing mixed integer and floating point numbers, the floating point values (float or double) are automatically converted to the double type when dividing.

You Might Also Like

Set Java Home

How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide

Check Your Java Version in the Windows Command Line

Use Easy Windows CMD Commands to Check Your Java Version

Compile & Run Java Program Using Command Prompt

Compile and Run Java Program by Notepad

How to Compile and Run Java Programs Using Notepad++

Calculate Percentage in Java

Add JARs to Project Build Paths in Eclipse (Java)

Check Null in Java

Install Java in Ubuntu Using Terminal

Program in Java

Create an Executable File from Eclipse

Call a Method in Java

Update Java

Check Java Version on a Mac

Print Double Quotes in Java

Simple Steps to Type a Bunny with Your Keyboard

About This Article

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 62,236 times.

Is this article up to date?

Quizzes

You Might Also Like

How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide

Use Easy Windows CMD Commands to Check Your Java Version

How to Compile and Run Java Programs Using Notepad++

Источник

Java 8 program for the division of two numbers

In this example, we will see “How to perform division of two numbers in Java 8?”. To achieve that, we are going to use BiFunction interface introduced in Java 8 as part of the functional interfaces and we will see it through several different ways.

Example 1. Java 8 code for division of two integer numbers (hard-coded integers)

System . out . println ( «Division of Two Numbers using BiFunction Interface — ‘apply’ example \n» ) ;

System . out . println ( «Division of 2 integer values — » + number1 + » and » + number2 + » is: » + biFunctionObj . apply ( number1 , number2 ) ) ;

Example 2. Java 8 code for division of two integer numbers (scanner input)

System . out . println ( «Division of Two Numbers using BiFunction Interface — ‘apply’ example \n» ) ;

System . out . println ( «\nDivision of 2 integer values — » + n1 + » by » + n2 + » is: » + biFunctionObj . apply ( n1 , n2 ) ) ;

Example 3. Java 8 code for division of two double numbers (scanner input)

System . out . println ( «Division of Two Double Numbers using BiFunction Interface — ‘apply’ example \n» ) ;

System . out . println ( «\nDivision of 2 double values — » + n1 + » by » + n2 + » is: » + biFunctionObj . apply ( n1 , n2 ) ) ;

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks. View all posts

Submit a Comment Cancel reply

Subscribe to our Newsletter

About Techndeck

Techndeck.com is a blog revolves around software development & testing technologies. All published posts are simple to understand and provided with relevant & easy to implement examples.

Techndeck.com’s author is Deepak Verma aka DV who is an Automation Architect by profession, lives in Ontario (Canada) with his beautiful wife (Isha) and adorable dog (Fifi). He is crazy about technologies, fitness and traveling etc. He runs a Travel Youtube Channel as well. He created & maintains Techndeck.com

This website uses cookies to improve your experience. We’ll assume you’re ok with this, but you can opt-out if you wish. Cookie settingsACCEPT

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

Источник

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

Источник

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