Arithmetic Operators


Arithmetic Operators

Addition, subtraction, multiplication, division, exponentiation, and modulus operations are all performed using the arithmetic operators. + This operator adds one operand to another.

The Java programming language offers different arithmetic operators for all floating-point and integer numbers. + (addition), - (subtraction), * (multiplication), / (division), and percent are the operators (modulo). The binary arithmetic operations of the Java programming language are summarised in the table below.

An operator does something with one or more operands. The following are some of the most frequent arithmetic operators:

Action Common Symbol
Addition +
Subtraction -
Multiplication *
Division /
Modulus (associated with integers) %

These are binary arithmetic operators, which means they have two operands. Constants or variables can be used as operands.

1 + age

There are two operands and one operator in this expression (addition). The first is a fixed value, while the second is a variable called age. If age had a value of 14, the formula would evaluate (or be comparable to) 15.

With the exception of division and modulus, these operators work exactly as you've memorized them. We generally think of division as a process that yields a partial response (a floating-point data type). However, the division may function differently when both operands are of the integer data type. Please see "Integer Division and Modulus" in the section below "Integer Division and Modulus."

Arithmetic Assignment Operators

Many computer languages offer the assignment (=) and arithmetic operators (+, -, *, /, percent). They're referred to as "compound assignment operators" or "mixed assignment operators" in some textbooks. The assignment operator and the arithmetic operators can be used to demonstrate their use. We'll use the variable age in the table, and you may assume it's an integer data type.

Pseudocode

Declare the type Integer.
Declare the variable Integer b.

Assign a = 3
Assign b = 2
Output "a = " & a
Output "b = " & b
Output "a + b = " & a + b
Output "a - b = " & a - b
Output "a * b = " & a * b
Output "a / b = " & a / b
Output "a % b = " & a % b

Output

a = 3
b = 2
a + b = 5
a - b = 1
a * b = 6
a / b = 1.5
a % b = 1

Example No.1:

<html> <body> <h2>Knowledge2life</h2> <p id="demo"></p> <script> let x = 100 + 50; document.getElementById("demo").innerHTML = x; </script> </body> </html>

OUTPUT:

Knowledge2life

150

Example No.2:

<html> <body> <h2>Knowledge2life</h2> <p id="demo"></p> <script> let a = 3; let x = (100 + 50) * a; document.getElementById("demo").innerHTML = x; </script> </body> </html>

OUTPUT:

Knowledge2life

450