In this post, we will learn an arithmetic operator in Java. Arithmetic operators in Java are required to perform arithmetic operations like addition, subtraction, multiplication, division, and modulus. We will learn all the available arithmetic operators in Java with an example of each.
List of arithmetic operator in Java
Let’s see the table that shows the detail of arithmetic operations:
Operator | Meaning | Example | Description |
+ | Addition | a+b | Adds a and b |
– | Subtraction | a-b | Subtracts b from a |
* | Multiplication | a*b | Multiplies a and b |
/ | Division | a/b | Divides a by b |
% | Modulus | a%b | Computes the remainder of dividing a by b |
Addition operator
The addition operator is denoted by + (plus) which is an arithmetic operator. It adds more than one number and returns the result. The following code adds variables a and b and assigns the result to variable c.
public class Addition {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c;
c = a + b;
System.out.println("The sum of " + a + " and " + b + " is " + c);
}
}
Output:
The sum of 10 and 5 is 15
Subtraction operator
The subtraction operator is denoted by – (minus) which is an arithmetic operator. It subtracts first number from the second number. The following code subtracts variable a from b and assigns subtracted result to variable c:
public class Subtraction {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c;
c = a - b;
System.out.println("The subtract from " + a + " to " + b + " is " + c);
}
}
Output:
The subtract from 10 to 5 is 5
Multiplication operator
The multiplication operator is denoted by * (asterisk) which is an arithmetic operator. This multiplies more than one number. The following code multiplies two variables a and b and assigns result to variable c.
public class Multiplication {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c;
c = a * b;
System.out.println("The multiple of " + a + " and " + b + " is " + c);
}
}
Output
The multiple of 10 to 5 is 5
Division operator
The division operator in Java is denoted by / (forward slash) which is an arithmetic operator. The following code divides variables a by b and assigns result to variable c.
public class Division {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c;
c = a / b;
System.out.println("The division of " + a + " by " + b + " is " + c);
}
}
Output
The division of 10 by 5 is 2
Modulo operator
The modulo operator in Java is denoted by % which is an arithmetic operator. The modulo operator produces the result as a reminder of the division of one number by another. The following code demonstrates the modulo operator dividing a variable a by b and assigns result to variable c.
public class Division {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c;
c = a / b;
System.out.println("The reminder of division " + a + " by " + b + " is " + c);
}
}
Output
The reminder of division 10 by 5 is 0
Conclusion
In this post, we learn the various arithmetic operators in Java.
Leave a Reply