Arithmetic operators are an essential part of any programming language, including Java. These operators allow you to perform various mathematical calculations such as addition, subtraction, multiplication, division, and more. In this article, we will explore the different arithmetic operators available in Java, along with examples and code explanations to help you understand their usage.
Java provides several arithmetic operators that you can use to perform mathematical operations. Here are the commonly used arithmetic operators in Java:
The addition operator (+) is used to add two values together. It can be used with numeric types such as integers (int), floatingpoint numbers (float and double), and characters (char). Here's an example:
int a = 5;
int b = 3;
int result = a + b;
System.out.println("Result: " + result);
Output
Result: 8
In the above code, we declare two variables a and b and assign them values. Then, we use the addition operator to add a and b together and store the result in the result variable. Finally, we print the result.
The subtraction operator () is used to subtract one value from another. It works similarly to the addition operator but performs subtraction instead. Let's see an example:
int a = 10;
int b = 7;
int result = a  b;
System.out.println("Result: " + result);
Output
Result: 3
In the above code, we subtract the value of b from a and store the result in the result variable.
int a = 4;
int b = 6;
int result = a * b;
System.out.println("Result: " + result);
Output
Result: 24
In the above code, we multiply the values of a and b and store the result in the result variable.
The division operator (/) is used to divide one value by another. It performs division and returns the quotient as a result. Let's see an example:
int a = 10;
int b = 3;
int result = a / b;
System.out.println("Result: " + result);
Output
Result: 3
In the above code, we divide the value of a by b and store the result in the result variable.
The modulus operator (%) is used to calculate the remainder of a division operation. It divides one value by another and returns the remainder. Here's an example:
int a = 10;
int b = 3;
int result = a % b;
System.out.println("Result: " + result);
Output
Result: 1
In the above code, we calculate the remainder when a is divided by b and store the result in the result variable.
The increment operator (++) is used to increase the value of a variable by 1. It can be used both as a prefix or postfix operator. Here's an example:
int a = 5;
a++; // Increment a by 1
System.out.println("Result: " + a);
Output
Result: 6
In the above code, we increment the value of a by 1 using the increment operator.
The decrement operator () is used to decrease the value of a variable by 1. It works similarly to the increment operator. Let's see an example:
int a = 5;
a; // Decrement a by 1
System.out.println("Result: " + a);
Output
Result: 4
In the above code, we decrement the value of a by 1 using the decrement operator.
Here are a few sample problems for you to practice using arithmetic operators:
Solutions:
1. Area of a rectangle = width * height
Output
Area of the rectangle: 40
2. Average of three numbers = (num1 + num2 + num3) / 3
int num1 = 12;
int num2 = 18;
int num3 = 25;
double average = (num1 + num2 + num3) / 3.0;
System.out.println("Average: " + average);
Output
Average: 18.333333333333332
3. Circumference of a circle = 2 * π * radius
double radius = 7;
double circumference = 2 * Math.PI * radius;
System.out.println("Circumference of the circle: " + circumference);
Output
Circumference of the circle: 43.982297150257104
4. Distance = speed * time
int speed = 60;
double time = 2.5;
double distance = speed * time;
System.out.println("Distance covered: " + distance + " km");
Output
Distance covered: 150.0 km
Arithmetic operators in Java allow you to perform various mathematical calculations. By understanding and utilizing these operators effectively, you can solve a wide range of mathematical problems in your Java programs. Remember to practice using the operators and experiment with different scenarios to strengthen your understanding.
60 videos37 docs12 tests


Explore Courses for Software Development exam
