The Java Arithmetic operators includes operators like Arithmetic Addition, Subtraction, Multiplication, Division and Modulus. All these operators are binary operators, which means they operate on two operands. Below table shows all the Arithmetic Operators in Java Programming language with examples.

Java Arithmetic Operators | Operation | Example |
---|---|---|

+ | Addition | 10 + 3 = 13 |

– | Subtraction | 10 – 3 = 7 |

* | Multiplication | 10 * 3 = 30 |

/ | Division | 10 / 3 = 3 |

% | Modulus – It returns the remainder after the division | 10 % 3 = 1 (Here remainder is One). |

**Example for Java Arithmetic Operators**

This Java Program allows the user to insert two integer variables a, b and we are going to use these two variables to perform various arithmetic operations present in Java Programming Language

**JAVA CODE**

// Program to Perform Arithmetic Operations in Java package JavaOperators; import java.util.Scanner; public class ArithmeticOperators { private static Scanner sc; public static void main(String[] args) { int a, b; int addition, subtraction, multiplication, division, modulus; sc = new Scanner(System.in); System.out.println("\n Please Enter two integer Value: "); a = sc.nextInt(); b = sc.nextInt(); addition = a + b; subtraction = a - b; multiplication = a * b; division = a / b; modulus = a % b; System.out.format("\n Addition of two numbers %d, %d is : %d\n", a, b, addition); System.out.format(" Subtraction of two numbers %d, %d is : %d\n", a, b, subtraction); System.out.format(" Multiplication of two numbers %d, %d is : %d\n", a, b, multiplication); System.out.format(" Division of two numbers %d, %d is : %d\n", a, b, division); System.out.format(" Modulus of two numbers %d, %d is : %d\n", a, b, modulus); } }

**OUTPUT**

**NOTE:** When we are using division ( / ) operator the result will be completely depend upon the data type it belongs to. For example, if the data type is integer then it will produce the integer value by rounding the value (7 / 3 = 2). If you want correct result then change the data type to float. Don’t get confused, let’s see one more example for better understanding

**Java Arithmetic Operators ****using Float**

For this example, We are using two variables a and b and their values are 25 and 4. We are going to use these two variables to show the problems we generally face while performing arithmetic operations on Int and Float Data type.

**JAVA CODE**

// Java Program to Perform Division and Modulus on Float data type package JavaOperators; public class ArithmeticOperatorsUsingFloat { public static void main(String[] args) { int a = 25, b = 4; int integerdiv, modulus; float floatdiv; integerdiv = a / b; modulus = a % b; floatdiv = (float)a / b; System.out.format(" Division of two numbers %d, %d is : %d\n", a, b, integerdiv); System.out.format(" Modulus of two numbers %d, %d is : %d\n", a, b, modulus); System.out.println(" \n---------Correct Results is--------- "); System.out.format(" Division of two numbers %d, %d is : %.3f", a, b, floatdiv); } }

**OUTPUT**

If you observe the above result we are getting two different results for same calculation because for the first result, both a and b are integers and the output is also integer (integerdiv) so, the compiler neglects the term after decimal point and shows answer 6 instead of 6.250 and a % b is 1 because remainder is 1. Next we changed output data type to float (floatdiv) and also converted the result to float to get our desired result.

**TIP:** Be careful while using division operator (Type casting plays major role here).

## Java Arithmetic Operators using Oops

In this example, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a methods.

**JAVA CODE**

package JavaOperators; public class ArithmeticOperator { int addition, subtraction, multiplication, division, modulus; public int addition(int a, int b) { addition = a + b; return addition; } public int subtraction(int a, int b) { subtraction = a - b; return subtraction; } public int multiplication(int a, int b) { multiplication = a * b; return multiplication; } public int division(int a, int b) { division = a / b; return division; } public int modulus(int a, int b) { modulus = a % b; return modulus; } }

Within the Main program, we will create an instance of the above specified class and call the methods.

**JAVA CODE**

package JavaOperators; import java.util.Scanner; public class ArithmeticOperatorsUsingClass { private static Scanner sc; public static void main(String[] args) { int a, b; int addition, subtraction, multiplication, division, modulus; sc = new Scanner(System.in); System.out.println("\n Please Enter two integer Value: "); a = sc.nextInt(); b = sc.nextInt(); ArithmeticOperator arith = new ArithmeticOperator(); addition = arith.addition(a, b); subtraction = arith.subtraction(a, b); multiplication = arith.multiplication(a, b); division = arith.division(a, b); modulus = arith.modulus(a, b); System.out.format("\n Addition of two numbers %d, %d is : %d\n", a, b, addition); System.out.format(" Subtraction of two numbers %d, %d is : %d\n", a, b, subtraction); System.out.format(" Multiplication of two numbers %d, %d is : %d\n", a, b, multiplication); System.out.format(" Division of two numbers %d, %d is : %d\n", a, b, division); System.out.format(" Modulus of two numbers %d, %d is : %d\n", a, b, modulus); } }

**OUTPUT**

**ANALYSIS**

**ArithmeticOperator Class Analysis: **Here we declared five functions of integer type and all those methods will accept two integer arguments. Within those function we are performing Arithmetic operations using Java arithmetic operators.

**TIP:** If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

**Main Class Analysis:**

First we created an instance / created an Object of the **ArithmeticOperator** Class

ArithmeticOperator arith = new ArithmeticOperator();

Next, we are calling all five method available in current instance. All those methods return an integer value so, we are assigning the return value to corresponding variables.

addition = arith.addition(a, b); subtraction = arith.subtraction(a, b); multiplication = arith.multiplication(a, b); division = arith.division(a, b); modulus = arith.modulus(a, b);

Lastly we used following System.out.format statement to print those java arithmetic operators

Thank You for Visiting Our Blog