# Java Arithmetic Operators

The Java Arithmetic operators include Addition, Subtraction, Multiplication, Division, and Modulus. All these Java Arithmetic Operators are binary, which means they operate on two operands.

The table below shows all the Arithmetic Operators in the Java Programming language with examples.

## Java Arithmetic Operators Example

This program allows the user to insert two integer variables, a and b. Next, we use these two variables to perform various arithmetic operations in Java Programming Language operators.

```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();

subtraction = a - b;
multiplication = a * b;
division = a / b;
modulus = a % b;

System.out.format("\nAddition 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);
}
}```
``````Please Enter two integer Value:
20
16

Addition of two numbers 20, 16 is : 36
Subtraction of two numbers 20, 16 is : 4
Multiplication of two numbers 20, 16 is : 320
Division of two numbers 20, 16 is : 1
Modulus of two numbers 20, 16 is : 4``````

When using a division ( / ) operator, the result will completely depend upon the data type it belongs to. For example, if the data type is an integer, it will produce the integer value by rounding it (7 / 3 = 2). To get the correct result, then change the data type to float. Please do not get confused; let us 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 will use these two variables to show the operator problems we generally face while performing Java arithmetic operations on Int and Float Datatype.

```// 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);
}
}```

If you observe the above result, we are getting two different results for the 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 the decimal point and shows answer 6 instead of 6.250, and a % b is 1 because the remainder is 1.

Next, we changed the output data type to float (floatdiv) and also converted the result to float to get our desired result. Be careful while using the division operator (Typecasting plays a major role here).

## Java Arithmetic Operators using Oops

In this example, we divide the code using Object-Oriented Programming. First, we will create a class that holds methods to do this.

```public class ArithmeticOperator {
int addition, subtraction, multiplication, division, modulus;

public int addition(int a, int b) {
}
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 of an Arithmetic Operators example, we will create an instance of the above-specified class and call the methods.

```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();
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);
}
}```
`````` Addition of two numbers 12, 3 is : 15
Subtraction of two numbers 12, 3 is : 9
Multiplication of two numbers 12, 3 is : 36
Division of two numbers 12, 3 is : 4
Modulus of two numbers 12, 3 is : 0``````

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

Main Class Analysis:

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

`ArithmeticOperator arith = new ArithmeticOperator();`

Next, we call all five methods available in the current Java instance. All those methods return an integer value, so we assign the return value to corresponding variables.

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