Scanner class Methods in Java

Java-Arithmetic Operators

Arithmetic Operators

In java, we can perform certain arithmetic and mathematical operations on the variables.These operations contains addition, subtraction,multiplication,division and finding the remainder of two numbers.

The operators that are used to apply arithmetical operations are knows as "Arithmetic Operators".
These operators can be used with any primitive datatype except boolean.These operators can be applied on two or more operands simultaneously.

Use of these operators is not a big deal.It requires simple math. These are simple mathematical symbols.

Arithmetic Operators used in Java

There are five arithmetic operators in  java ,they are as follows:

  1. Addition (+): Addition (+) symbol is used to add two or more values of  data or variables.It gives sum of numbers. For example,   a+b, It is expression which we can write to add to numbers "a" and "b" where a and b are variables and can have any value. We can also simply write two numbers, for example, 5+6. Now lets see example using code: 
    Arithmetic Operators example in Java
    In example code, Comments explains everything, you can see the output in the console on the Right side,It prints result in every new line as I've used "println()" method which prints the results in next line each time.
  2. Subtraction (-): Subtraction (-) symbol is used to subtract two or more values of  data or variables.It gives the difference of numbers. For example,   a-b, It is expression which we can write to subtract two numbers "a" and "b" where a and b are variables and can have any value. We can also simply write two numbers, for example, 5-6. Now lets see example using code: 
    Arithmetic Operators example in Java

    In example code, try to understand the code by yourself , you can see the output in the console on the Right side.
  3. Multiplication(*): Multiplication (*) symbol is used to multiply two or more values of data or variables.It gives the product of numbers. For example a*b, It is expression which we can write to multiply two numbers "a" and "b" where a and b are variables and can have any value. We can also simply write two numbers, for example, 5*6. Now lets see example using code: 
    Arithmetic Operators example in Java


    In example code, try to understand the code by yourself , you can see the output in the console on the Right side.
  4. Division(/): Division (/) symbol is used to divide two or more values of data or variables.It gives the quotient of numbers.The number on left side of division symbol is numerator which is divided by the right side number called denominator.  For example,   a/b, It is expression which we can write to divide two numbers "a" and "b" where a and b are variables and can have any value. We can also simply write two numbers, for example, 6/2. Here, 6 will get divide by 2 and quotient is printed as result of division.Now lets see example using code: 
    Arithmetic Operators example in Java

     Note: If you divide any number with 0 then it will throw an "DivieByZeroException" which is "ArithmeticException" and your program will not work. And  if  you divide 0 by any other number then it will print 0. Try this by Yourself to understand this concept.
    In example code, try to understand the code by yourself , you can see the output in the console on the Right side.
  5. Modulus(%):- This operator is very different than the previous operators.This operator is used to calculate the remainder after dividing two numbers. The modulus operators divides two numbers and  gives the remainder instead of quotient.Sometimes we need to make logic by finding remainders in program,that time is the best operator to work with.It is simple to use.For example,  "a%b" will divide "a" with "b" and returns the remainder.Take another example,"6%4" will return "2" as remainder.Lets see example with code:
    Arithmetic Operators example in Java


Note: The result of division of one integer with other is referred to as Integer Division as  this operation results in a quotient without a decimal point.If division carries out with two floating point numbers or one floating point number and one integer,the result will be in floating point number.

Example: This example will illustrate all operators and with different data types.

Arithmetic Operators example in Java

Keep Practicing ,Happy Coding!



Comments