Arithmetic Operators in Java

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.

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

Syntax:

num1 + num2 

Example

num1 = 10, num2 = 20;

add = num1 + num2; // result 30


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

Syntax:

 num1 - num2 

Example

num1 = 30, num2 = 20;

sub = num1 - num2; // result  10

 

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

Syntax:

 num1 * num2 

Example

num1 = 10, num2 = 20;

mult = num1 * num2;//result 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;// result 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;// result 1

 

No comments:

Post a Comment