![]() |
VOOZH | about |
Arithmetic operators in Java are used to perform mathematical calculations on numeric values. They are among the most commonly used operators in programming and help perform operations such as addition, subtraction, multiplication, division, and finding remainders. These operators work with Java's primitive numeric data types like int, float, double, long, and others.
👁 Arithmetic Operators in Java
Now let's look at each one of the arithmetic operators in Java:
The addition operator is a binary operator that adds two operands and returns their sum.
Syntax:
operand1 + operand2
num1 = 10 num2 = 20 The sum = 30
The subtraction operator is a binary operator that subtracts the second operand from the first operand.
Syntax:
operand1 - operand2
num1 = 20 num2 = 10 Subtraction = 10
The multiplication operator is a binary operator that multiplies two operands and returns their product.
Syntax:
operand1 * operand2
num1 = 20 num2 = 10 Multiplication = 200
The division operator is a binary operator that divides the first operand by the second operand and returns the quotient.
Syntax:
operand1 / operand2
num1 = 20 num2 = 10 Division = 2
The modulus operator is a binary operator that returns the remainder after dividing the first operand by the second operand.
Syntax:
operand1 % operand2
num1 = 5 num2 = 2 Remainder = 1
Example: Program in Java that implements all basic arithmetic operators for user input:
Enter the first number: 20
Enter the second number: 10
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: This program uses the Scanner class to take two numbers as input from the user. It reads the values using nextDouble(), performs basic arithmetic operations such as addition, subtraction, multiplication, and division, and stores the results in separate variables. Finally, it displays the calculated results using System.out.println(). This demonstrates how arithmetic operators can be used with user input in Java.