Java Arithmetic Operators with Examples

Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types:

- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operators

This article explains all that one needs to know regarding Arithmetic Operators.

### Arithmetic Operators

These operators involve the mathematical operators that can be used to perform various simple or advanced arithmetic operations on the primitive data types referred to as the operands. These operators consist of various unary and binary operators that can be applied on a single or two operands. Let’s look at the various operators that Java has to provide under the arithmetic operators.

Now let’s look at each one of the arithmetic operators in Java:

1. Addition(+): This operator is a binary operator and is used to add two operands.

Syntax:

num1 + num2

Example:

num1 = 10, num2 = 20 sum = num1 + num2 = 30

## Java

num1 = 10 num2 = 20 The sum = 30

2. Subtraction(-): This operator is a binary operator and is used to subtract two operands.

Syntax:

num1 – num2

Example:

num1 = 20, num2 = 10 sub = num1 – num2 = 10

## Java

num1 = 20 num2 = 10 Subtraction = 10

3. Multiplication(*): This operator is a binary operator and is used to multiply two operands.

Syntax:

num1 * num2

Example:

num1 = 20, num2 = 10 mult = num1 * num2 = 200

## Java

num1 = 20 num2 = 10 Multiplication = 200

4. Division(/): This is a binary operator that is used to divide the first operand(dividend) by the second operand(divisor) and give the quotient as a result.

Syntax:

num1 / num2

Example:

num1 = 20, num2 = 10 div = num1 / num2 = 2

## Java

num1 = 20 num2 = 10 Division = 2

5. Modulus(%): This is a binary operator that is used to return the remainder when the first operand(dividend) is divided by the second operand(divisor).

Syntax:

num1 % num2

Example:

num1 = 5, num2 = 2 mod = num1 % num2 = 1

## Java

num1 = 5 num2 = 2 Remainder = 1

Here is an example program in Java that implements all basic arithmetic operators for user input:

## Java

#### Input

Enter the first number: 20 Enter the second number: 10

#### Output

The sum of the two numbers is: 30.0 The difference of the two numbers is: 10.0 The product of the two numbers is: 200.0 The quotient of the two numbers is: 2.0

### Explanation

- The program implements basic arithmetic operations using user input in Java. The program uses the Scanner class from the java.util package to read user input from the console. The following steps describe how the program works in detail:
- Import the java.util.Scanner class: The program starts by importing the Scanner class, which is used to read input from the console.
- Create a Scanner object: Next, a Scanner object sc is created and associated with the standard input stream System.in.
- Read the first number from the user: The program prompts the user to enter the first number and uses the nextDouble() method of the Scanner class to read the input. The input is stored in the num1 variable of type double.
- Read the second number from the user: The program prompts the user to enter the second number and uses the nextDouble() method of the Scanner class to read the input. The input is stored in the num2 variable of type double.
- Perform arithmetic operations: The program performs the four basic arithmetic operations (addition, subtraction, multiplication, and division) using the num1 and num2 variables and stores the results in separate variables sum, difference, product, and quotient.
- Print the results: The program prints out the results of the arithmetic operations using the println method of the System.out object.
- This program demonstrates how to implement basic arithmetic operations using user input in Java. The Scanner class makes it easy to read user input from the console, and the basic arithmetic operations are performed using standard mathematical operators in Java.